-
Notifications
You must be signed in to change notification settings - Fork 46.4k
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
Why useEffect's default behavior is to run on every render? #17454
Comments
By the API design: useEffect(
() => {
// do something
},
[/* dependency list */]
);
|
Suppose you are very new to React, and you want to do something whenever a
I think the dependency list is intended to be more of a performance optimization/aid to code simplification. |
The current API allows for you to decide between the following behaviors:
The "default" behavior you describe (when you don't explicitly specify the dependencies) is often the safest since it prevents stale values from being used in closures. In the future, hopefully we will provide some type of compiler to help automate a lot of this away. In the meantime, we provide an official ESLint plug-in to help make it a little easier. 😄 |
Do you want to request a feature or report a bug?
API design question about
useEffect
What is the current behavior?
Currently
useEffect
runs on every render. This default behavior can be dangerous in situations like dealing with HTTP requests when you forget to pass the second argument. This seems to be a common mistake especially for newcomers like myself. I can't think of many (any) patterns where you want to runuseEffect
on every render. What was the reasoning behind not defaulting to run once?The text was updated successfully, but these errors were encountered: