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.

Handling componentWillReceiveProps deprecation in React >= 17

See original GitHub issue

The docs currently reference componentWillReceiveProps as the recommended way to listen to route changes when using shallow routing:

You can watch for URL changes via componentWillReceiveProps hook as shown below:

componentWillReceiveProps(nextProps) {
  const { pathname, query } = nextProps.url
  // fetch data based on the new query
}

This lifecycle, however, is on the deprecation path; it will stop working as-named in React 17: https://reactjs.org/docs/react-component.html#unsafe_componentwillreceiveprops

Given that, we should provide a more future-friendly solution for listening for shallow route changes. Perhaps adding a popstate listener in componentDidMount. If that seems like the best option I can add it to docs, but opening this ticket for discussion/tracking.

Issue Analytics

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

github_iconTop GitHub Comments

3reactions
timneutkenscommented, Apr 20, 2018

Related to react-hot-loader. We have to update to rhl 5.1 for it to work correctly, but I want to do some testing first

2reactions
timneutkenscommented, Apr 15, 2018

react-hot-loader is also using componentWillReceiveProps in AppContainer right now btw.

Read more comments on GitHub >

github_iconTop Results From Across the Web

React17, or how to get rid of “componentWillReceiveProps”?
It should return an object to update the state, or null to update nothing. This method is useful only for state handling, and...
Read more >
Handle 'componentWillReceiveProps' deprecation warning
As part of the implementation of async rendering in React, there are a few lifecycle methods which will be deprecated in React 17...
Read more >
React 17 lifecycle - Medium
The React team has decided to deprecate some of the lifecycle methods with React 17 ... It's main job is replacing componentWillReceiveProps.
Read more >
React.Component
In the event of an error, you can render a fallback UI with componentDidCatch() by calling setState , but this will be deprecated...
Read more >
Refactor componentWillReceiveProps() to ... - Egghead.io
The componentWillReceiveProps() method is being deprecated in future version of React (17). Many of us use this method day-to-day to check for incoming...
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