Next.js Discord

Discord Forum

State management VS Cookies/LocalStorage/Session

Unanswered
Canaan Dog posted this in #help-forum
Open in Discord
Canaan DogOP
Hello all, for my question I will use cookies, local storage and sessions, as the same thing even though they are not.
I am wondering and I would like to here your opinions on the following matter.

I am developing a custom eshop with nextjs and I am in a deadend. For the cart functionality I am not sure if I should use only cookies or go with both cookies and a state manager like Zustand.
What are your thoughts on this? Is there a reason to use both a state manager and cookies for a cart functionality? Are cookies enough?

Thanks in advance,
Andreas

11 Replies

@Ray I would use cookies to store cart id and the other data will be stored in db
Canaan DogOP
My idea before started thinking about state management was this exactly. Store a cartID as a cookie and then fetch everything else using server actions with the stored cartID.
yeah that's cool
Canaan DogOP
What I am wondering is this. Am I missing something with this path? Maybe it could be better to have a state manager to be fast and agile in the front and then the db will be updated from state manager and store also a cart id in the cookie. e.g. this way, when user adds a product in cart, the local cart in state will be updated and then the cookie and the db will updated later even if it fails.

Of course it is more possible I am overthinking this 😅
I don't think you need a state management there?
you could add the product to cart with server action
btw, here is a good example from vercel
Canaan DogOP
Thank you for your valuable input @Ray and the link! 🙂