-
-
Notifications
You must be signed in to change notification settings - Fork 1.5k
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
Is it possible to immediately load persisted data? #346
Comments
I thought about conditionally making the middleware's hydration sync or async. But I don't know if people would find this confusing or not. |
@AnatoleLucet We're addressing the same concern here with Jotai as well. Right now the Jotai Maybe we could both add a fourth parameter I'm curious what @dai-shi thinks as well. |
I think it's possible if we check the return value of |
Will this also work with async deserialization and migration functions? |
@aidan-keay yes, we would just need to check each of these three. |
I have noticed this problem too, as the SSR will render a page with the default persisted state, and not trigger a re-render with the actual persisted state. |
I bumped into this after I started injecting a zustand store through context as shown in #182 (comment). Somewhere in the children of the store's provider, I have a UPDATEI managed to work around the issue with a local boolean state and the function initialiseStore(onRehydrated: () => void) {
return create<MyConfig>(
persist(
(set, get) => ({ ... }),
{
name: 'my-config',
onRehydrateStorage: () => onRehydrated,
}
)
);
}
export function MyConfigProvider(props: { children: ReactNode }) {
const { children } = props;
// https://github.com/pmndrs/zustand/issues/346
const [reydrated, setRehydrated] = useState(false);
// https://reactjs.org/docs/hooks-reference.html#lazy-initial-state
const [useStore] = useState(() => {
return initialiseStore(() => setRehydrated(true));
});
return reydrated ? <Provider value={useStore}>{children}</Provider> : null;
} |
Thanks everyone! Usage:
The provider allows a override of the persistent options and a fallback component whilst rehydrating. Implementation:
|
Should be fixed in 3.5.2 by #403. |
Hi there, we are currently facing this issue with |
Hey @chhourbro, it seems to work as expected on |
I am using React Native's AsyncStorage, and facing this issue. |
@jankrah12 If you want to do some kind of gate to prevent your app from rendering if your zustand store has not yet been hydrated with const useStore = create(
persist(
(set, get) => ({
// ...
_hasHydrated: false
}),
{
name: 'store',
onRehydrateStorage: () => () => {
useStore.setState({ _hasHydrated: true })
}
}
)
);
export default function App() {
const hasHydrated = useStore(state => state._hasHydrated);
if (!hasHydrated) {
return <p>Loading...</p>
}
return (
// ...
);
} Edit: since zustand v3.6.3, you can now know if the store has been hydrated using |
this didn't work for me
had to go for
|
actually, i've now completely removed the rehydration guard. this is because i'm using react-native-mmkv as storage engine, which is a synchronous storage. The question is then, do we need to guard for hydration only on asynchronous storage engines ? |
You shouldn't need to check for hydration if you're using a sync storage. See this part of the doc for more details. FYI, this is probably why you had an error when trying to use |
FYI: This is not true, if you're using Next.js with Static Site Generation ( #324 Explains this more in detail |
This is not correct. We have to pass the setHydrated property. Like this: onRehydrateStorage:
({ setHydrated }) =>
state => {
setHydrated(true);
} |
It will work with produce / immer middleware
|
I'm using the persist middleware and loading state from local storage. As far as I am aware, there is a brief period where the store state is the default state and then the store is rehydrated with the persisted state. Is there any way to load this immediately if the storage access method is synchronous? It works if I load from localStorage manually at the top of the file and set this as the store state although I would rather use the middleware.
The text was updated successfully, but these errors were encountered: