zustand: store.setState() api method doesn't trigger persistance

I’m trying to use persistance middleware in React, however I’m doing store updates from outside a react component by calling useStore.setState({ property: value }). This function call sets the state and appears in Redux devtools without persisting the update in localStorage.

About this issue

  • Original URL
  • State: closed
  • Created 4 years ago
  • Comments: 16 (7 by maintainers)

Most upvoted comments

Yeah, I think that’s only the option.

devtools middleware has this code:

    const savedSetState = api.setState
    api.setState = (state: PartialState<S>, replace?: boolean) => {
      savedSetState(state, replace)
      api.devtools.send(api.devtools.prefix + 'setState', api.getState())
    }

I’m pretty sure if there’s just one persist middleware alone, it works fine. When we chain multiple middleware, we need to be careful.

I’m not sure. We’ll definitely need to modify some bits in vanilla.ts -> create(). An option would be to modify the store’s internal api object via the middleware (same thing as set) and then use it in create(). But I’m not sure how this could be done, I’ll check later in the day.