Next.js Discord

Discord Forum

How to global state?

Black Caiman posted this in #help-forum
Open in Discord
Black CaimanOP
I'm using app dir, i have a context and it wrapps children in the rootLayout.
Here is the problem:
When i do some product filteration in the home page and try to change website language, then my context state is resetting.
Second one, i filtered and selected one product and going to buy this. In transitioned page, my context state is reseted.
So when i change the route, my whole app is rerendered and my global state reseted.
How to avoid this?

4 Replies

Yellow Crazy Ant
Hey @Black Caiman i'm facing the same issue, as i'm redirecting to another page, state inside context are resetting.. please let me know if you've found a solution to this ?
Asian black bear
Jotai has a local storage atom that works great
Yellow Crazy Ant
the context is resetting and my navigation is based on state value of context...
with localstorage i'm using useEffect and changing context state with localstorage value,

but it takes some delay since its resetting to false and setting again to true since i'm logged in
Yellow Crazy Ant
i tried with zustand, it gave me hydration error... so i'm manually setting the localstorage, maybe i'll use zustand for future states,

thankyou so much