Next.js Discord

Discord Forum

Zustand on server side

Answered
rex1410 posted this in #help-forum
Open in Discord
Avatar
Guys, when using zustand, I read this thread where people said that using zustand states on server side is a problematic approach with hidden bugs. But can I instantiate the zustand state on server side? Fetch from DB -> Set first Zustand State -> send to client side. Will this work without any issues?
Answered by rex1410
found an even better guide down in the docs, for the exact scenario https://docs.pmnd.rs/zustand/guides/nextjs
View full answer

8 Replies

Avatar
you could fetch the data on server and pass the data as props then provide it to zustand store with context api
https://docs.pmnd.rs/zustand/guides/initialize-state-with-props
Avatar
@Ray thanks for the link, I missed this.
Avatar
found an even better guide down in the docs, for the exact scenario https://docs.pmnd.rs/zustand/guides/nextjs
Answer
Avatar
@Ray @joulev any idea about this one? its like a follow up doubt for this.. https://nextjs-forum.com/post/1225330229539504168
Avatar
Prairie yellowjacket
hey did you manage to find a good solution?
Avatar
yes
the link I shared above is the thing
In short, you create a React Context, and provide state using that