bsr
bsr

Reputation: 58692

How to set context with React.cloneElement

With cloneElement, one can set new props like:

React.cloneElement(node, { ...props });

Is there a way to set context also? Or is the only way to pass context to wrap it in a React component, and set childContextTypes and getChildContext on this container?

Upvotes: 2

Views: 1829

Answers (1)

Ori Drori
Ori Drori

Reputation: 192016

Since react 0.14 context is based on the parent, so cloning a component doesn't clone context, because the context is not part of the component. This official answer explains that change.

In addition to the parent configuration (childContextTypes and getChildContext), your component should also include contextTypes to use context.

Upvotes: 2

Related Questions