-
-
Notifications
You must be signed in to change notification settings - Fork 10.3k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Unsafe Lifecycle warning in React >= 16.3 #6060
Comments
For Router, we could write like this to fix the render() {
const { children } = this.props;
return (
<RouterContext.Provider value={this.getChildContext()}>
<HistoryListener history={this.props.history} onLocationChange={() => { this.setState(...) }} />
{children ? React.Children.only(children) : null}
</RouterContext.Provider>
);
} class HistoryListener extends React.Component<{ history: History, onLocationChange: () => void }> {
unlisten: any = null
componentDidMount() {
this.unlisten = this.props.history.listen(() => {
this.props.onLocationChange()
})
}
componentWillUnmount() {
if (this.unlisten) {
this.unlisten()
}
}
render() {
return null
}
} |
This should be fixed in v3 too. |
@morlay No need to farm that out to another component. Instead, we can move to cDM and simply do a second check for if the location changed during that time. Another option, if we wanted to go all-in on 16+, would be to use a setState callback function for the initial read from history. |
Easy next step: Upgrade to React 16.3 locally and add a failing test, like I did here: reduxjs/react-redux#918 |
@timdorr
Then we can have logic in I tried providing another callback method in context to omit location changes or proxy history |
That's fine. We can add checks to avoid double execution. |
Is there a PR to fix this? |
I opened a PR, but I need someone to help me test this. |
Created #6341 which hopefully is on the path of resolving this issue. |
WOOOO! |
@mjackson as I said #6060 (comment) if we need to add listener in this.unlisten && this.unlisten()
this.unlisten = ....
```; |
Yeah, that listen call should be in a cDM. It's going to leak in server rendering too. |
Version
react-router@create-context-api
[email protected] under React.StrictMode
Steps to reproduce
use react-router under React.StrictMode
Expected Behavior
without warning
Actual Behavior
warning for
componentWillReceiveProps
componentWillMount
The text was updated successfully, but these errors were encountered: