NextJS and zustand #2326
-
In the new nextjs guide, it say: Does this mean we can't use the slices pattern? or do we create a general store provider that handles all the slices? Sorry, just having a hard time wrapping my head around zustand best practices and nextjs. Any advice would be awesome. If anyone has a repo or a tutorial of implementing zustand into a nextjs app with multiple stores or multiple slices that would be incredible, thanks! |
Beta Was this translation helpful? Give feedback.
Replies: 8 comments 32 replies
-
Ive been trying to implement based off the guide, and Im running into lots of little issues, so a more comprehensive tutorial and an example repo would be incredibly helpful I imagine. Some issues Ive run into are that createContext requires a default value, and useStore is not imported in the guide's code. There are a couple others, but those might be my implementations problem. |
Beta Was this translation helpful? Give feedback.
-
Also, after going through jotai documentation, creating a zustand provider to avoid all this set up would be awesome |
Beta Was this translation helpful? Give feedback.
-
@alexander-densley what issues are you facing right now? about slices I don't see why you can't use slice pattern. We are only saying that you should avoid shared store across requests due to that could lead you to some issues even if you don't use Zustand that's something could happen if you are using a global store/variable or module state like Zustand. |
Beta Was this translation helpful? Give feedback.
-
@dbritto-dev I guess one of the things Im currently confused with is the implementation of these custom providers. Do we have to make a provider for each slice we create, cause if the case thats quite the headache? and if so where and how should they be wrapped around layouts and components. I would love to see a more comprehensive guide explaining whats going on because it is a lot more hands on and boilerplate than traditional zustand |
Beta Was this translation helpful? Give feedback.
-
I'm a bit confused by this statement after reading the Next.js integration guide. Does this only concern server-side (Node.js environment) only or both server and client? I'm using Zustand in my Next.js projects (pages directory) as a global store, but only on the client-side, nothing on the server, and from what I've seen each request has its own store. So I was wondering if I should also migrate to using Zustand with React Context. P.S Thank you for creating/maintaining this awesome library! |
Beta Was this translation helpful? Give feedback.
-
Hey @alexander-densley, I have been struggling with this as well but got it working after some time. I created a little library that summarizes all of the boilerplate of working with I also thought about making a pull request to add this to the thir-party-libraries section in the docs, but I am not sure it is with it, as it really is about 100 lines of code, what are your thoughts? Maybe part of this could be implemented directly into zustand, to make it easier to create a context provider, and the |
Beta Was this translation helpful? Give feedback.
-
i am also facing the same issues since last few days. Can't get the head around on how to use multiple store slices along with context-providers & RSC. Any example or instructions help would be highly appreciated. Thank you. |
Beta Was this translation helpful? Give feedback.
-
Can anyone explain how the tsconfig from the doc (https://docs.pmnd.rs/zustand/guides/nextjs#creating-a-store-per-request) makes the store created per request? Would it be fine if I don't use the same tsconfig? |
Beta Was this translation helpful? Give feedback.
@HasanMothaffar sure, let me explain you two scenarios:
On nextjs any component is rendered/initialized twice, one on the backend and one the client.
When is safe but:
As you can see …