You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
Hooks: Calling setState with the SAME value multiple times, evaluates the component function up-to 2 times #17474
Using the State hook, calling setXYZ() with the same value multiple times causes the component function to be evaluated more than once (up-to 2 times). I don't mean re-rendered, but a re-evaluation of its function.
Scenario 1 (OK):
the component function is evaluated, initial render useState(1)
click button => setState(1)
the component function is not re-evaluated. ✔️
click button again => setState(1)
the component function is not re-evaluated.
click button again => setState(1)
the component function is not re-evaluated.
etc.
Scenario 2:
the component function is evaluated, initial render useState(1)
click button => setState(2)
the component function IS re-evaluated, component re-rendered ✔️
changed the title [-]Hooks: Calling setState with the SAME value multiple times, evaluates the component function up-to 2 times?[/-][+]Hooks: Calling setState with the SAME value multiple times, evaluates the component function up-to 2 times[/+]on Nov 27, 2019
This is a known quirk due to the implementation details of concurrency in React. We don't cheaply know which of two versions is currently committed. When this ambiguity happens we have to over render once and after that we know that both versions are the same and it doesn't matter.
Reacted by Heaven, Gunn, Minh Nguyen, Will Tonkin, Wenchen Li, Peter, Ronald, fanqi, Max Fontani, Sergei Kirjanov and 8 more
In that fiddle, the App function is run one extra time after calling setValue with the same value, but the result of the last function call, is not being sent to the dom. Also notice how useEffect is not calling its function after that last function call (which makes sense, because there wasn't any effect in the dom with that last function call).
I do not consider this a bug. Just something I didn't expect at the time. To be precise; I didn't expect the App function to be called the last time (when setValue was called with the same value). The explanation by @sebmarkbage makes sense though. Is this behaviour documented somewhere?
This issue has been automatically marked as stale because it has not had recent activity. It will be closed if no further activity occurs. Thank you for your contribution.
Activity
[-]Hooks: Calling setState with the SAME value multiple times, evaluates the component function up-to 2 times?[/-][+]Hooks: Calling setState with the SAME value multiple times, evaluates the component function up-to 2 times[/+]AjaxSolutions commentedon Nov 27, 2019
Interesting discovery. This certainly doesn't look like a useful feature.
sebmarkbage commentedon Dec 3, 2019
This is a known quirk due to the implementation details of concurrency in React. We don't cheaply know which of two versions is currently committed. When this ambiguity happens we have to over render once and after that we know that both versions are the same and it doesn't matter.
christiaanwesterbeek commentedon Dec 18, 2019
I encountered the same thing while trying to understand React hooks better.
Check out this fiddle, and open up the Console tab. https://jsfiddle.net/devotis/jd12g7y4/
In that fiddle, the App function is run one extra time after calling setValue with the same value, but the result of the last function call, is not being sent to the dom. Also notice how useEffect is not calling its function after that last function call (which makes sense, because there wasn't any effect in the dom with that last function call).
I do not consider this a bug. Just something I didn't expect at the time. To be precise; I didn't expect the App function to be called the last time (when setValue was called with the same value). The explanation by @sebmarkbage makes sense though. Is this behaviour documented somewhere?
stale commentedon Mar 18, 2020
This issue has been automatically marked as stale because it has not had recent activity. It will be closed if no further activity occurs. Thank you for your contribution.