-
-
Notifications
You must be signed in to change notification settings - Fork 3.3k
Description
(I can provide a reproducible example if there's a simple test project that I can base it on.)
Essentially, I'm using React 16.6 and doing this:
const MyContext = React.createContext();
@connect(...)
class MyComponent extends React.Component {
static contextType = MyContext;
render() {
return <div>Hello {this.context}</div>;
}
}
ReactDOM.render(<MyContext.Provider value="world"><MyComponent/></MyContext.Provider/>, ...);
And it fails with an error. In my concrete example, the value isn't "world" of course, but it happens to be undefined, so I'm getting a TypeError: cannot read property 'store' of undefined here because context===undefined. I guess if I were to pass "world" as the context, it would instead fail the invariant just below.
The problem is that the <Connect(MyComponent)> component gets its context populated based on my static contextType, even though it's setting its own legacy context for the usual redux plumbing. But the new context takes precedence. This happens because <Connect(MyComponent)> declares the legacy contextTypes and additionally hoists the contextType from the nested component. This is ultimately a bug in the hoist-non-react-statics library and I have already sent a fix there: mridgway/hoist-non-react-statics#62
Please let me know if you disagree with the fix. In case they don't merge it soon, react-redux could work around the issue by doing something like this (I can send a PR):
const hoisted = hoistStatics(Connect, WrappedComponent);
delete hoisted.contextType;
return hoisted;