Next.js Discord

Discord Forum

Unexpected effect trigger in nested context on fast navigation

Unanswered
German Hound posted this in #help-forum
Open in Discord
German HoundOP
I have a root-level context in my Next.js app that handles and stores the global application state. Inside a nested route, I want to load data specific to that route in its layout (which is a server component), then pass that data into a context provider specific to the route. In that route-level context, I use both the data from the layout and state from the root context.

In the root context, I have an effect that depends on the pathname and runs some logic involving a server action.

The issue I'm facing is: when I navigate too quickly — for example, by double-clicking a <Link> — before the server action in the root context finishes, the layout of the nested route doesn't get reloaded, but the useEffect inside the nested context still gets triggered. As a result, the state that I previously set in that route context gets reset unexpectedly.

[demo](http://v0-test-two-lemon-25.vercel.app/)
[Github repos](https://github.com/taphuocanh94/test)

0 Replies