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

useEffect gets called twice #1919

Open
calebeby opened this issue Jul 7, 2022 · 0 comments
Open

useEffect gets called twice #1919

calebeby opened this issue Jul 7, 2022 · 0 comments
Labels

Comments

@calebeby
Copy link
Member

calebeby commented Jul 7, 2022

In almost all of our stories which use useEffect, we are using it inside of a function embedded in JSX like this:

{() => {
  useEffect(() => {
    // ...
  }, []);
}}

Storybook changed the number of times the function in JSX is called. Now useEffect gets called twice, even when we meant for it to be called once.

This is a problem for stories which might attach event listeners inside useEffect: we only want those listeners to get attached once, but they would get called multiple times.

We can fix this by moving the useEffect into a decorator:

(story) => {
  useEffect(() => {
    // ...
  }, []);
  return story();
},

To see an example of this change, see #1918

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

No branches or pull requests

1 participant