Persisting state across Next.js pages
Does anyone know the best way to persist state across pages, I want to have a cart with items that persists on all pages, I have tried zustand but no luck.
3 Replies
Reacts context api can be used to store state values globally. If you’re talking solely about rendering a UI component across all pages, simply render that component in the highest part of your component tree (I believe this is _app.tsx in the pages dir, not sure what the file is called for app dir)
Thanks for reply, I ended up by using zustand with persistance , works perfectly!
you have a SPA that maintains its state across pages (unless you use
<a>
links which reload the page) so you can put your shared state in the app, which persists between pages