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.

Connected components cannot use new 16.3 lifecycles

See original GitHub issue

I know that there is still much consideration going into https://github.com/reactjs/react-redux/issues/890 so I don’t mean to rush, but I thought you might be interested to know:

It looks like React 16.3 exposes the new lifecycles in a way that disallow for using the old componentWill* at the same time as new lifecycles.

Something that looks roughly like this:

class Form extends PureComponent {
  constructor(props) {
    super(props);
    this.state = {initialized: false};
  }
  static getDerivedStateFromProps(nextProps, prevState) {
    if (nextProps.data && !prevState.initialized) {
      return {initialized: true};
    }
    return null;
  }
  render() {
    return this.state.initialized ? "initialized" : "uninitialized"
  }
}
export default connect()(Form);

will get this warning:

screen shot 2018-03-30 at 9 24 26 am

which I didn’t understand until I saw that the implementation details of connect use inheritance to add those lifecycles.

My workaround in this first case I found in my code is pretty easy to fix (I was just using the connect to get a dispatch method) but I bet people are going to start running into this as they upgrade to 16.3.

Sending my best to your efforts in https://github.com/reactjs/react-redux/issues/890 !

Issue Analytics

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

github_iconTop GitHub Comments

3reactions
timdorrcommented, Apr 20, 2018

@vctormb cDU is the recommended way: https://reactjs.org/blog/2018/03/27/update-on-async-rendering.html#side-effects-on-props-change

But I’d probably ask what your cWRP is responding to. Could that be responsible for dispatching the action? Could you push the state transformation back to the store itself? There is probably a larger refactor that will actually put you in a better position not just for React async stuff, but as your app changes over time.

3reactions
gaearoncommented, Apr 20, 2018

No, gDSFP must be pure and contain no dispatch calls.

Dispatching in cDU is okay-ish. Although it’s still not that great from performance perspective to dispatch in response to re-rendering. But you were already doing it.

Read more comments on GitHub >

github_iconTop Results From Across the Web

React17, or how to get rid of “componentWillReceiveProps”?
As you may already know, with the release of React 16.3, some of legacy lifecycles were deprecated. One of the biggest lessons we've...
Read more >
Replacing 'componentWillReceiveProps' with ' ...
With the release of React 16.3, some new lifecycle methods have been introduced, and release of React 17 will deprecate some lifecycle method....
Read more >
React 16.3 Context: expected a string (for built-in ...
I'm rather new to React, so this might be a very simple thing. Basically, I've been trying to operate modals using Context. I've...
Read more >
A beginners guide to the React component lifecycle
However, React 16.3 is currently in Alpha (as of when this was written). You can use the constructor method or componentDidMount depending ...
Read more >
What is StrictMode in React?
What is React StrictMode and How to Use It? What Does Strict Mode Help With? ... New lifecycles, such as getDerivedStateFromProps and ...
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