-
-
Notifications
You must be signed in to change notification settings - Fork 1.6k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
A swapped in store will not update reactively #721
Comments
Oh,,, I overlooked this. (Hm, why does react-hooks eslint rule warn this? Maybe, because it's in a create function.) Can you also try if #550 fixes this issue? |
@zzmp I'm very sorry. I didn't correctly understand what this is about, went ahead to #722, and thought it's fine. But, this is not something we want to allow. It's like conditionally invoking hooks, which is basically violating the rule of hooks. We shouldn't dynamically create a hook in a component. It just happen to work because the useStore hook has the same implementation, but in general it's not guaranteed, and it looks like a bad practice. zustand v3 has a workaround with In your use case, what's easy is to remount the component: <MyComponent key={unique_string_for_mock} storeBackedProvider={mock} /> I agree this is a non-trivial limitation and zustand v4 with #550 will provide a new API for this use case. So, please be patient. My big apologies again for your work in #722 with my wrong understanding. |
For someone interested, the way I see is this. // this is creating a hook dynamically in render
const useStore = create(props.createState)
const value = useStore()
// which is technically the same as this (which looks like a bad practice)
const value = props.condition ? useState(0) : useState(1) |
@dai-shi if the behavior will change in #550, and #722 will introduce the new behavior early without any breaking changes to v3, would it be ok to merge #722 so that the new behavior can be used as part of v3 too? We were hoping to use zustand because it lets you initialize stores outside of the React lifecycle, but this point - while it does look like an antipattern - removes that "feature" that we wanted out of zustand, by forcing us to initialize the store with the react component. |
To my understanding #550 works by chance, and we don't guarantee the usage for the future. So even when #550 is merged, that's not a recommended usage. Doesn't When v4 lands, we can use |
I've switched to 4.0.0-beta.0, and it does solve my case, without any extra work. Do you know why it does so? |
Because 4.0.0-beta.0 includes #550. Note again that my recommendation is to use |
Closing this. |
I'm trying to use
zustand
for a project where a user can specify azustand
-backed provider. If the user switches their provider, any changes to the new providers store are not picked up by react. This is because any uses of thezustand
hookuseStore
will not be updated to listen to the new provider, because the listener is set up once, with no deps.As a contrived (untested) example:
In this case, if the
mock
is swapped out,MyComponent
will no longer be reactive tovalue
.I propose depending on
api
when registering listeners to fix this.The text was updated successfully, but these errors were encountered: