jhonnymichel / react-hookstore

A state management library for react using the bleeding edge hooks feature

Home Page:https://codesandbox.io/s/r58pqonkop

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Memory leak in concurrent mode

pabra opened this issue · comments

Looks like you have the same isse, I had.
Reported in pabra/react-hooksack#1 by @andreasgruenh.

Hello, @pabra! Thank you for your tip!
But, unfortunately I have tried the same solution that you used and that dit not work ):

See these lines of code:

  useEffect(() => () => {
    store.setters = store.setters.filter(setter => setter !== set)
  }, [])

  if (!store.setters.includes(set)) {
    store.setters.push(set);
  }

You are pushing the set function into the global setters array on render but remove it in the useEffect cleanup.
You have to push the set function into the global state in the useEffect call so that setup und cleanup are always in sync.

  useEffect(() => {
    store.setters.push(set);
    return () => {
      store.setters = store.setters.filter(setter => setter !== set)
    }, [])

@andreasgruenh Yes, I'm trying this solution locally. Here is my code:

export function useStore(identifier) {
  const store = getStoreByIdentifier(identifier)
  if (!store) {
    throw "store does not exist"
  }

  const [state, set] = useState(store.state)

  useEffect(() => {
    if (!store.setters.includes(set)) {
      store.setters.push(set)
    }

    return () => {
      store.setters = store.setters.filter(setter => setter !== set)
    }
  }, [])

  return [state, store.setState]
}

But this solution keep throwing a warning about memory leak 😕
I'm using the library on a Gatsby site, and when I change page the warning comes out.

Hello, everyone!
Well, I said before that even after the fixing the memory leak continued, but I was wrong. My code was generating another memory leak, and that was not react-hookstore's fault.

I'll open a PR with the fixed suggested by @pabra and @andreasgruenh

Thanks a lot @kaueburiti, @pabra and @andreasgruenh