Home > Enterprise >  browser back button does not re-render component after router.push
browser back button does not re-render component after router.push

Time:03-11

Scenario :

I am on Page A which have a X component that has state sunny = false initially. after I did some operation in component; state is updated to sunny = true. now I route.push(Page B). when I navigate back to Page A using browser back button I see component X has stale state 'sunny' = true. I want it to reset to sunny = false.

Is it bug in nextjs route? If not how can I force particular component to re-render so not using stale states

CodePudding user response:

You can pass query parameters as below:

Router.push({
    pathname: '/about',
    query: { name: 'Someone' }
})

and access it to change the state

CodePudding user response:

As far as I know, this is not a bug.

Depending on the router that you use, the solution can be different. But this is the "JavaScript way" of handling your routing.

componentDidMount() {
  // to make sure window.onpopstate is fired went his page is opened.
  this._isMounted = true;
  window.onpopstate = () => {
    if(this._isMounted) {
      // set state to initial state.
    }
  }
}

source: https://stackoverflow.com/a/39363278/7552329

  • Related