bsr bsr - 4 months ago 44
React JSX Question

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 only way to pass context is to wrap it in a react component, and set
childContextTypes
and
getChildContext
on this container ?

Answer

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.