Keep the store data during hot reload? #827
Replies: 2 comments
-
You mean when editing the store file? Supporting it as a library would require babel plugin for babel, and something else for something else, which would be too much effort. As a user, you could save your store somewhere, for example export const useStore = create((set) => ({
count: 0,
...window.savedStore,
inc: () => set((state) => ({ count: state.count + 1 }),
}))
useStore.subscribe((state) => {
window.savedStore = state
}) (Not tested...) |
Beta Was this translation helpful? Give feedback.
0 replies
-
For posterity, this works for me with a Vite project: declare global {
interface Window {
__store: AppStoreState;
}
}
if (import.meta.hot) {
useStore.subscribe((state) => {
if (typeof window !== "undefined") {
window.__store = state;
}
});
import.meta.hot!.accept((newModule) => {
if (!newModule) return;
const newStore = newModule.useStore;
if (!newStore) return;
if (window.__store) {
newStore.setState(window.__store, true);
}
});
} |
Beta Was this translation helpful? Give feedback.
0 replies
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
-
Is there a way to keep the store data during hot reload? This seems like a common enough use case, probably someone has some code on hand. Nextjs
Beta Was this translation helpful? Give feedback.
All reactions