next.js: Page state is not reset for navigating between dynamic routes
Bug report
Describe the bug
The page state is not reset for navigation between dynamic routes that served by the same source component.
for example, give page source /a/[param]/index.js, when navigating from /a/1 to /a/b, states on the page won’t’ be reset.
The causing is that for this kind of navigation, what actually happened is the same React Component been rendered with different props. Thus react takes it as a component is rerendering itself, and causing the new navigated page receive stale states.
To fix it, just add {key: <composed base on URL routing params> } to page initial props.
To Reproduce
I’ve created a live example:
Expected behavior
Page should be fully reloaded and states should be reset.
Screenshots
N/A
System information
- OS: ALL
- Browser: ALL
- Version of Next.js: 9.1.6
Additional context
About this issue
- Original URL
- State: closed
- Created 7 years ago
- Reactions: 89
- Comments: 52 (8 by maintainers)
Commits related to this issue
- Solved an issue where page state is not reset Due to an issue in NextJS the state of a page might not be properly reset when navigating. Through the use of an event listener this can be mitigated. S... — committed to tzengerink/annekelabordus.nl by deleted user 6 years ago
- Fix NextJS link not working properly This is a bug in NextJS, bug report here: https://github.com/vercel/next.js/issues/9992 — committed to le-minhphuc/web-Phong-Kham by le-minhphuc 6 years ago
- Fix NextJS link not working properly This is a bug in NextJS, bug report here: https://github.com/vercel/next.js/issues/9992 — committed to le-minhphuc/web-Phong-Kham by le-minhphuc 6 years ago
- Fix NextJS link not working properly This is a bug in NextJS, bug report here: https://github.com/vercel/next.js/issues/9992 — committed to le-minhphuc/web-Phong-Kham by le-minhphuc 6 years ago
Great find, suggest docs are updated. For future devs, same should be applied in
getStaticProps()Fixed with some help from the Discord community - the issue for me was the key sent to my
<Component>in_app.jswas wrong, i was previously using:<Component {...pageProps} key={router.route} />updating to:
<Component {...pageProps} key={router.asPath} />fixed it for me
Yes! I’ve seen similar confusing behavior when using
useEffect.It only fires once, even when you change the page, but stay on the same route. I’m not sure if it’s wrong, but it’s definitely not intuitive in my opinion. It’s non-intuitive because it works differently when changing the page, but coming from a different route.
I have just come across a solution in my case.
My eg. is involving a link from one
page/[id1]/something.jsto some otherpage/[id2]/something.jsThe solution for me was:
Im still having This issue when using getServerSideProps, any updates?
Great… and now the question is… why this isn’t a default setting?
PS: @samuelgoddard great site design 😉
This works thanks a ton ❤️
Custom _app.tsx with
<Component {...pageProps} key={router.asPath} />definitely solved my issue. Wish I had landed on this issue before killing so many hours on this.As @valse point out, why isn’t this the default? @Timer, could this be considered?
You literally saved me hours of pain. I was trying to use effect cleanups to get around my issue but this is all I needed. Thank you.
OMG this keyed solution is magic! But shouldn’t this be the default behavior for dynamic routes? In my blog post page I had some “refresh” issues navigation client side and I didn’t understood why… until now 😛
Confirming that
Produced the expected mount/unmount effects that @Janpot mentioned.
I’ve been having this same issue and been able to mostly solve it (in many places) using react
keyproperties on my markup that contain the dynamic route value… For example…For a route that uses a
[lang]property to change language… e.g./[lang]/page1Setting the unique key based on the property seems to let react know to re-render the items… it seems even if the data of the page is different, when there is the same fundamental component, react doesn’t know to properly update the components on the page. Figured I’d share what seems to be working for me in a lot of cases.
This worked for me as well. Thank you. To add, just for completeness, the router is the value defined using useRouter
The working solution is that from @samuelgoddard adding the
router.asPathkey to the_apppage Component:<Component {...pageProps} key={router.asPath} />Also experiencing issues with
getStaticPropsand browser back, even after adding a uniquekeyto thepropsreturn.This feels so wrong but it’s working and I can’t find a better solution.
Using
nextversion 10.0.4.The problem with using the key prop in getStaticProps is that, while going from one page with dynamic props to another one with different props will work, going back (using window.history.back()) will only show the last page with dynamic props that was reached.
/a/1 ---> /a/2 ---> /a/3 ---> /a/4 ---> /a/5This will work ok with key in props./a/5 <--- /a/5 <--- /a/5 <--- /a/5 <--- /a/5This is how the pages will render when using history.back() while having key in props.Any solution?
@BananaWanted I just got this exact issue and thanks to @Janpot he pointed me to this post and tried your solution and it worked for me.
I’m not sure if this is a “next router” issue or a react issue, but even with your workaround/solution sometimes the browser back button doesn’t work as expected, so I think this should be fixed.