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.

How to cleanup store on component unmount?

See original GitHub issue

I couldn’t find a way to cleanup my store on component unmount. I’m using the Zustand way mentioned in the docs for handling stores with createTrackedSelector, but it seems like this hook keeps the reference to the store forever without providing a way to avoid memory-leaks.

const useMyStore = createTrackedSelector(MyZustandStore)

const MyComponent = () => {
  const store = useMyStore()
  useEffect(() => {
    return () => {
     // how can I destroy my store on cleanup?
   }
  }, [])
  return <button onClick={store.increase}>{store.count}</button>
}

calling MyZustandStore.destroy() doesn’t help; neither does re-assigning the store with another value.

Issue Analytics

  • State:closed
  • Created a year ago
  • Comments:6 (3 by maintainers)

github_iconTop GitHub Comments

1reaction
dai-shicommented, Jun 3, 2022

That will become like a local state, but something like the following.

export const useZustandStore = (
  storeFactory
) => {
  const ref = useRef();
  if (!ref.current) {
    const useStore = storeFactory();
    const useTrackedStore = createTrackedSelector(useStore);
    ref.current = useTrackedStore;
  }
  return ref.current;
};

I used useRef instead of useState, because it’s slightly more lightweight.

0reactions
Sinakhxcommented, Jun 3, 2022

This is nice. Thanks for sharing!

Read more comments on GitHub >

github_iconTop Results From Across the Web

Clean redux state when component unmount cancel all ...
Clean redux state when component unmount cancel all subscriptions and asynchronous tasks in a useEffect cleanup function.
Read more >
Understanding React's useEffect cleanup function
React's useEffect cleanup function saves applications from unwanted behaviors like memory leaks by cleaning up effects.
Read more >
How to Cleanup Side Effects in React | by ReactOne - Medium
The solution is to cancel any side effect if the component unmounts, let's see how to do that in the next section. 2...
Read more >
React useEffect cleanup: How and when to use it
Can't perform a React state update on an unmounted component. This is a no-op, but it indicates a memory leak in your application....
Read more >
Why you should always Cleanup Side Effects in React ...
Why should you cleanup when a Component unmounts? · To avoid memory leaks · To optimize our applications for a good user experience...
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