The Problem: Changing the parameters of a <Route />
component does not update the component it is rendering. The route change is shown in the URL bar, but directly rendering {this.props.match.params.id}
shows the old :id
and not the new one reflected in the URL bar.
Update: I fixed this by moving the <BrowserRouter />
out from the index.js file and into the App.js file. It is no longer the direct child of Provider and is instead the child of the App component. No clue why this makes everything suddenly work.
What I am doing: I have a <Link to="/user/11" />
that goes from user/7
(or any current ID) to a /user/11
The componentWillReceiveProps(newProps)
of the component it is rendering is not fired.(This component is connected using react-redux
if that helps any. I tried applying withRouter
around the connection and that did not help)
If I manually refresh the page in chrome (using CTRL-R or the refresh button) the page shows the new data, rendering the "new" param.
TLDR: Switching from /user/7
to /user/11
does not fire that componentWillRecieveProps
function and therefore leaving the component displaying the old state
Question: What am I doing incorrectly here that causes componentWillReceiveProps
to not fire.
I am using react-router v4 and the latest create-react-app
This is my CWRP function:
componentWillReceiveProps(newProps) {
console.log("getProps")
this.props.getUser(newProps.match.params.id)
if (newProps.match.params.id == newProps.currentUser.id) {
this.setState({ user: "currentUser" })
} else {
this.setState({ user: "selectedUser" })
}
}
This is the full code of my component: https://gist.github.com/Connorelsea/c5c14e7c54994292bef2852475fc6b43
I was following the solution here and it did not seem to work for me. Component does not remount when route parameters change