Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Remove state from dep array of useStore's useEffect #226

Open
wants to merge 1 commit into
base: main
Choose a base branch
from

Conversation

antonisprovidakis
Copy link

I couldn't figure out any obvious reason for why state was added in the dep array of the useEffect used in useStore, since it's not used inside it.

I removed it and everything keeps working as before (i.e. unit tests pass, manual testing seems just fine).

Actually, things got a bit better since the useEffect does not run for every state update.
It only runs once: on mount.

Before

The useEffect runs 7 times (for each subscriber) for the initial mount and all state transitions of a single toast.

before.mp4

After

The useEffect runs 1 time (for each subscriber) only for the initial mount.

after.mp4

@vercel
Copy link

vercel bot commented Oct 1, 2022

The latest updates on your projects. Learn more about Vercel for Git ↗︎

Name Status Preview Updated
react-hot-toast ✅ Ready (Inspect) Visit Preview Oct 1, 2022 at 7:23PM (UTC)

}, [state]);
}, []);

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

👍 this looks good to me.

Question: should setState be in the deps list instead? I don't think useState ever returns a different "setter" function after the first invocation but it could.

e.g.

  useEffect(() => {
    listeners.push(setState);
    return () => {
      const index = listeners.indexOf(setState);
      if (index > -1) {
         listeners.splice(index, 1);
       }
     };
   }, [setState]);

If we were using eslint we could add the react-hooks/exhaustive-deps rule to check for these issues.

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm pretty sure, react-hooks/exhaustive-deps won't "yell" at us for omitting setState from the dep array.
It's guaranteed (by React) that setState will be stable for the lifetime of the component.

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ah yes you are correct:

React guarantees that setState function identity is stable and won’t change on re-renders. This is why it’s safe to omit from the useEffect or useCallback dependency list.

https://reactjs.org/docs/hooks-reference.html#usestate

@antonisprovidakis
Copy link
Author

@timolins If this is not going to be merged for some reason, should I close it then?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

2 participants