question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

@connect + contextType breaks with React 16.6

See original GitHub issue

(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: https://github.com/mridgway/hoist-non-react-statics/pull/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;

Issue Analytics

  • State:closed
  • Created 5 years ago
  • Reactions:3
  • Comments:12 (6 by maintainers)

github_iconTop GitHub Comments

2reactions
CrOrccommented, Oct 30, 2018

Until react-redux v6 is released the easiest workaround is to wrap component with contextType into function component

const MyContext = React.createContext();

class MyComponent extends React.Component {
    static contextType = MyContext;
    render() {
        return <div>Hello {this.context}</div>;
    }
}
const MyConnectedComponent = connect(...)(props=><MyComponent {...props/>)
2reactions
markeriksoncommented, Oct 28, 2018

This is likely due to one of the known bugs in React where mixing legacy context and createContext cause things to work wrong. Also, while it may not be directly related, we don’t recommend or support using connect as a decorator.

If there’s an easy fix, we can potentially look at it, but otherwise my advice is to avoid using contextType on a component until React-Redux v6 is out, and you’ve upgraded to that.

Read more comments on GitHub >

github_iconTop Results From Across the Web

React v16.6.0: lazy, memo and contextType
Today we're releasing React 16.6 with a few new convenient features. ... This sometimes breaks parent components that don't expect their ...
Read more >
So What's New in React v16.6?. Introducing ... - Bits and Pieces
React 16.6 comes with contextType in a component which makes it convenient to consume a context value from within a class component without ......
Read more >
React 16.6.0 Goodies - CSS-Tricks
React.memo() avoids unnecessary re-rendering. There are situations where a component re-renders, even if neither its state nor its props changed ...
Read more >
React 16.6 new features: contextType, lazy, memo
In this blog post, I am going to assume that you already have certain experience with React, and have been working with React...
Read more >
React / Context API / TypeScript : How to init web app and ...
export class AppContainer extends Component { static contextType = AppContext componentDidMount() { /** If user is not connected, verify if a ...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found