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

fix(useSetState): memoize setState callback #557

Merged
merged 2 commits into from
Aug 23, 2019
Merged

fix(useSetState): memoize setState callback #557

merged 2 commits into from
Aug 23, 2019

Conversation

TylerR909
Copy link
Contributor

Addresses #555

Wraps the callback from useSetState in useCallback to memoize it so that folks following the Best Practices from the React FAQ with the react-hooks/exhaustive-deps rule enabled don't trigger endless rerenders in a useEffect(() => {}, [setState]); where setState is constantly changing out from under them.

I tried to write a couple tests for this but I'm not really sure what angle to approach it from or how to layer everything together to test for something useful.

Problematic code fixed by this PR:

const myComponent = () => {
  const [{ loading, data }, setState] = useSetState({ loading: true, data: null });

  useEffect(() => {
    console.log('hi');
    // fetch data
    setState({ loading: false, data: 'data!' });
  }, [setState])

  return null;
}

image

@Belco90
Copy link
Contributor

Belco90 commented Aug 23, 2019

Ey, thanks for your fix! Testing this should be easy: at the end you need to check if the returned setState fn is always the same instance, so you can add an additional test to useSetState.test.ts like 'should return always same setter instance' where:

  1. render the hook and save its returned setState in a const
  2. rerender the hook and save new returned setState in a new const
  3. compare two saved setState are the same with jest matcher toBe as expect(Object.is(received, expected)).toBe(true)

It should fail with the previous code and pass with your new fix. You can find an example about how rerender a hook within usePrevious.test.ts file.

Copy link
Contributor

@Belco90 Belco90 left a comment

Choose a reason for hiding this comment

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

Add commented test for checking function instance is always the same

@TylerR909
Copy link
Contributor Author

👍

Added a test to do just that. Validated that it broke with the old code and passes with the new code.

Hope it meets with your approval. Thanks!

Copy link
Owner

@streamich streamich left a comment

Choose a reason for hiding this comment

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

Thanks!

@streamich streamich merged commit 0275329 into streamich:master Aug 23, 2019
@streamich
Copy link
Owner

🎉 This PR is included in version 11.0.2 🎉

The release is available on:

Your semantic-release bot 📦🚀

@TylerR909 TylerR909 mentioned this pull request Nov 20, 2019
15 tasks
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants