Next.js Discord

Discord Forum

Custom Context Provider state reset on routing

Unanswered
Mexican Jay posted this in #help-forum
Open in Discord
Avatar
Mexican JayOP
On a new react context, I am trying to isolute the state and have defined the state within a custom provider of the context with copies of the state values and setters placed into the context, however routing in nextjs (Link even with shallow), seems to recreate the custom provider and reset all state to the initial values on each route. I was expecting my context provider to be create (its state set to initial values once)

My provider is similar to that of the academind nextjs course by custom context provider
https://github.com/mschwarzmueller/nextjs-course-code/blob/09-context/store/notification-context.js

Using that as an example, I would expect that the "activeNotification" state variable would keep it's value between route navigations (assuming the timer has not yet triggered)

8 Replies

Avatar
Mexican JayOP
My NextJS version # is 14.1.0
And adding shallow on the Link does not help.
Avatar
Mexican JayOP
Using Page Router if that matters.
Can someone confirm if this is a bug? And if yes, how do I report it? (And if no, does NextJS with Page Router (or with another package if needed) have a solution? (Currently I've worked around this by moving the state and setters to the App, but that is really poor design.)
Avatar
cadco_ka
I'm using next-intl.

I don't understand the issue with async. If it's asynchronous, it doesn't work to use translation, which is not allowed. Is there a workaround?
Avatar
Mexican JayOP
@cadco_ka Your response does not appear to me as connected to my question.
Avatar
cadco_ka
I got it to work now .. missunderstandin in the doc
Avatar
josh
Could you share the code that uses the NotificationContextProvider?