How to persist xstate/store #5156
Answered
by
davidkpiano
julianklumpers
asked this question in
Q&A
-
Do i have to convert my xstate store to a state machine first to persist my state? |
Beta Was this translation helpful? Give feedback.
Answered by
davidkpiano
Dec 25, 2024
Replies: 1 comment 1 reply
-
You do not: you can subscribe to the store and create the store with restored context: // Restore state
const restoredState = getPersistedState();
const store = createStore({
context: restoredState.context ?? {
// default initial context
},
on: {
// ...
}
});
// Persist state
store.subscribe(state => {
persist(state);
}); |
Beta Was this translation helpful? Give feedback.
1 reply
Answer selected by
julianklumpers
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
You do not: you can subscribe to the store and create the store with restored context: