Skip to content

How to persist xstate/store #5156

Answered by davidkpiano
julianklumpers asked this question in Q&A
Discussion options

You must be logged in to vote

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);
});

Replies: 1 comment 1 reply

Comment options

You must be logged in to vote
1 reply
@julianklumpers
Comment options

Answer selected by julianklumpers
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Category
Q&A
Labels
None yet
2 participants