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
Docs: "manually ensure the state is updated using useEffect" is vague and hidden #35138
Comments
The current docs were written by @ijjk in #26320. (Before then, there was nothing documenting this behavior?!) @ijjk could you expand a bit on what you meant by "ensure the state is updated using Also: are there ways we could avoid other devs going on multi-hour debugging sessions, only to eventually find this note after debugging? E.g.
|
I also think the documentation should explain why the current behavior is as it is -- what's the advantage of it? Can I do anything cool with the current behavior, that makes up for its surprisingness? Currently the note in the docs reads like a bug description. |
This seems like a React confusion though, not Next.js specific.
When you navigate between pages, the page component receives the new props ( Adding this would work: useEffect(() => {
setTitle(pageTitle)
}, [pageTitle]) but it's unnecessary in your example's case. If you rely on the updated I believe our docs is clear enough around this, but let me know how would you change it based on the above. 👍 Maybe https://nextjs.org/learn/basics/navigate-between-pages/client-side could indeed have a note around this. |
Note that my example above is artificially minimal. My real application is much larger, with pages using a Zustand react context, which is initialized using Next.js props. I was very surprised to find that the Zustand context was not re-initialized when navigating to a new page. In the example above, I replaced all this with just a I'm not sure that using |
Let's expand that note to also show a code snippet. 👍 Do you have a good suggestion? |
Hey. Just an add-on. I was just going through the issues. I agree with @balazsorban44 That is the whole point of hooks i guess. I might be wrong in understanding your point. |
I've opened a PR that explains the issue and shows ways to work around it. I still think this is an important enough conceptual issue that it should be in the tutorial, but let's start with the docs. |
* Docs: workarounds for router not resetting state (fixes #35138) * Apply suggestions from code review * updates Co-authored-by: JJ Kasper <jj@jjsweb.site>
This closed issue has been automatically locked because it had no new activity for a month. If you are running into a similar issue, please create a new issue with the steps to reproduce. Thank you. |
What is the improvement or update you wish to see?
In the docs for next/router I read:
I think there are two problems here:
useEffect
? What should it do, and when? Is there an example anywhere?Is there any context that might help us understand?
Here is a minimal test case that I expected to work:
I expected that navigating between
/one
andtwo
would refresh the state. However, it does not, and so the page title remains the title of the initially loaded page.My expectation came from the assuming that the identity (or React key) of the current component is the current URL. In other words, I expected the default
app.tsx
to look something like this:In fact, using the above as my
_app.ts
seems to fix the issue. What problems are there with this approach?Does the docs page already exist? Please link to it.
https://nextjs.org/docs/api-reference/next/router
The text was updated successfully, but these errors were encountered: