question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

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

See original GitHub issue

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.

Issue Analytics

  • State:closed
  • Created 3 years ago
  • Comments:16 (7 by maintainers)

github_iconTop GitHub Comments

1reaction
dai-shicommented, Nov 10, 2020

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.

1reaction
AnatoleLucetcommented, Nov 10, 2020

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.

Read more comments on GitHub >

github_iconTop Results From Across the Web

Calling setState without triggering re-render - Stack Overflow
while this solution works and new render won't be called, I would strongly advise against modifying state directly and using setState() every ...
Read more >
How to Save State to LocalStorage & Persist on Refresh with ...
LocalStorage is a browser API that allows developers to store data ... news is this method will work for any data that you...
Read more >
How To Manage State in React with Redux - DigitalOcean
Redux is a popular data store for JavaScript and React applications. ... to work offline and load faster, you can change // unregister()...
Read more >
Recipes - Zustand Documentation - Pmndrs.docs
You can, but bear in mind that it will cause the component to update on every state change! const state = useStore(). Selecting...
Read more >
Accessing React State right after setting it - DEV Community ‍ ‍
Yes but No, at this point, you are just praying ) that setState ... be able to access event argument as shown in...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found