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

setField in useEffect dependency causes infinite loop #105

Closed
keeganstothert opened this issue Sep 5, 2019 · 3 comments · Fixed by #116
Closed

setField in useEffect dependency causes infinite loop #105

keeganstothert opened this issue Sep 5, 2019 · 3 comments · Fixed by #116
Labels
bug Something isn't working (confirmed)
Milestone

Comments

@keeganstothert
Copy link

eslint automatically adds setField as a dependency in any useEffect that uses it. This causes an infinite loop. currently using // eslint-disable-next-line to prevent this behaviour. react-use-form-state isn't the only library i've seen that causes this so maybe it's something on my end or my understanding of hooks, but I feel like setField should be stable the same way dispatch in redux is a stable dependency

@keeganstothert keeganstothert changed the title setField as useEffect dependency causes infinite loop setField in useEffect dependency causes infinite loop Sep 5, 2019
@alterx
Copy link

alterx commented Sep 17, 2019

Here's a simple CodeSandbox that shows the issue:

https://codesandbox.io/s/amazing-germain-k2gy0

@philipeatela
Copy link

I guess it makes sense that running setField should be a stable dependency (don't know much about how to achieve this) but there is a possible workaround for the situation displayed on the codesandbox posted by @alterx

You can add a condition inside the fetchData method to run your fetch + setField only if: a) The current input form fields are empty; b) Neither of them have been touched by the user yet

This way I believe it works as expected with no eslint errors.
Codesandbox: https://codesandbox.io/embed/gifted-haibt-13o4f

@wsmd wsmd added bug Something isn't working (confirmed) under review The issue is being reviewed by the maintainer labels Oct 19, 2019
@wsmd wsmd added this to the 0.12.0 milestone Oct 19, 2019
@wsmd wsmd closed this as completed in #116 Oct 19, 2019
@wsmd wsmd removed the under review The issue is being reviewed by the maintainer label Oct 19, 2019
@wsmd
Copy link
Owner

wsmd commented Oct 23, 2019

This issue is now fixed in v0.12.0! 🎉

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working (confirmed)
Projects
None yet
Development

Successfully merging a pull request may close this issue.

4 participants