Wait for rehydration of multiple stores #426
-
Hi, I'm very new to zustand and my understaing is that it promotes using multiple stores vs something like redux that uses only one. My question is can you share a code sample on how to guard the app from rendering before all stores are rehydrated, when using the persist middleware. persist offers an onRehydrateStorage prop, but this would only be needed on the first useStore call const store = set => ({
isAuthenthicated: false,
authenticate: ....
})
const options = {
name: 'auth-storage',
getStorage: () => AsyncStorage,
onRehydrateStorage: () => <onRehydrated>
}
const useStore = create(persist(store, options))
So I need to send this function down to create the store, but only on first use, at the root component perhaps which holds state to determine whether the stores are hydrated or not. But in other parts of the code I will not need to check for rehydration. I hope this makes sense |
Beta Was this translation helpful? Give feedback.
Replies: 4 comments 3 replies
-
I think I have answered my own question I have a rehydrated prop in the store and a setRehydrated function. In the persist config I whitelist all properties needed except for the In the components that consume the stores, I guard the rendering by checking the rehydrated prop of all the stores. it works :) |
Beta Was this translation helpful? Give feedback.
-
Trying to develop a similar setup as the above, but I'm unable to access and modify state in a onRehydrate function. How can I modify the state? Using the
|
Beta Was this translation helpful? Give feedback.
-
My onRehydrateStorage is
|
Beta Was this translation helpful? Give feedback.
-
I tried both methods supplied above. I still cannot access the state effectively. I'm receiving "TypeError: undefined is not an object (evaluating 'useStore.getState')":
I've also tried:
But useStore is returning an undefined object. |
Beta Was this translation helpful? Give feedback.
I think I have answered my own question
I have a rehydrated prop in the store and a setRehydrated function.
In the persist config I whitelist all properties needed except for the
rehydrated
prop.In the components that consume the stores, I guard the rendering by checking the rehydrated prop of all the stores.
it works :)