#makePersisted createStore

5 messages · Page 1 of 1 (latest)

stoic spade
#

makePersisted works with stores as well afaik

#

makePersisted(createStore(...))

#

also you can write your own
getting the initial value is just JSON.parse(localstorage.getItem("my-key"))
and syncing changes can be done by intercepting all writes

const wrappedSetAccount: typeof setAccount = (...args: any[]) => {
  setAccount(...args)
  const state = unwrap(account)
  localstorage.setItem("my-key", JSON.stringify(state))
}
#

then you can add validation logic
in case you want to make sure that the state read from storage has the shape you expect

grand ravine