In zustand 4.5.1, useStore.setState(...) does not work correctly. #2355
-
|
If you were previously using version 4.4.7 and then versioned up to 4.5.1, useStore.setState is not working correctly. The version I'm currently using in my project looks like this (I'm not very good at English, so I used a translator to help me out). |
Beta Was this translation helpful? Give feedback.
Replies: 2 comments 3 replies
-
|
@willy-dunamu I encourage you to read our new nextjs guide. But let's talk about the new behavior which is the right one: on previous versions we use to use getState to get the initial data but wasn't the initial data since getState can be modified to return something else so we introduce getInitialState that gets a snapshot of initial state and can't be modified imperatively just declaratively using useEffect, this means you can't read/write from states on server and that's why you can't use setState on server to set the initial state. Everything is working as expected and is not an issue. |
Beta Was this translation helpful? Give feedback.
-
|
Thank you for your kind response. Attached is a file showing how the experience differs between 4.4.7 and 4.5.1. |
Beta Was this translation helpful? Give feedback.
@willy-dunamu well you need to understand that the initial state shouldn't be affected or modified, there's no thing like setting up the state before the initial render your component is render twice one on the server and one the client so keep that in mind. if you want to re-initialize your store with the data from the server you have two options: create the store with data or use
useEffectto setup the initial data. BTW, keep in mind the rules and recommendations that we had in the nextjs guide, like you should create the store per request and you shouldn't use global stores.