I'm using react router v4, had some issue reloading the page (not window.location.reload). I better give a real use case to explain the issue, we use a social network app as the example:
- user A commented a post by user B, a notification appear in user B page.
- user B clicked on the notification, we do
this.props.history.push('/job/' + id')
, it worked, hence user B went tojob/123
page. - user A commented again, new notification appear in user B page, while user B still remain on the
job/123
page, he clicked on the notification link and triggeredthis.props.history.push('/job' + id')
. But he won't see the page rerender, he DID NOT see the latest comment because the page does nothing.
You are describing 2 different kinds of state changes.
In the first scenario, when user B is not at the
/job/:id
page and he clicks a link you get a URL change, which triggers a state change in the router, and propagates that change through to your component so you can see the comment.In the second scenario, when user B is already at the
/job/:id
page and a new comment comes through, the URL doesn't need to change, so clicking on a link won't change the URL and won't trigger a state change in the router, so you won't see the new content.I would probably try something like this (pseudo code because I don't know how you're getting new comments or subscribing via the websocket):
Now the page will get updated in both scenarios.
It seems to be a common scenario in many cases. It can be tackled using many different approaches. Check this stackoverflow question. There are some good answers and findings. Personally this approach made more sense to me.
location.key
changes every single time whenever user tries to navigate between pages, even within the sameroute
. To test this place below block of code in you/jod/:id
component:I had this exact same situation. Updated state in
componentDidUpdate
. After that worked as expected. Clicking on items within the same route updates state and displays correct info.I assume (as not sure how you're passing/updating comments in
/job/:id
) if you set something like this in your/job/:id
component should work:You should watch updating
postId
incomponentWillUpdate
life cycle and do something from here like this: