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
When calling a setState with an updater function inside a useEffect, the additional call made in Strict Mode is not ignored, producing an unexpected state. The same behavior is NOT seen in React 17. From the docs:
In Strict Mode, React will call your updater function twice in order to help you find accidental impurities. This is development-only behavior and does not affect production. If your updater function is pure (as it should be), this should not affect the behavior. The result from one of the calls will be ignored.
React version: 18
Steps To Reproduce
Create a state that holds a number.
Include a call to its setState inside a useEffect, which adds +1 to the previous value.
Note that React will NOT ignore the additional call made in Strict Mode and will sum twice.
In React 18, StrictMode started double invoking effects. This is why you're seeing an additional call of the state updater function because we're invoking the function passed to useEffect again which calls setState again.
When calling a setState with an updater function inside a useEffect, the additional call made in Strict Mode is not ignored, producing an unexpected state. The same behavior is NOT seen in React 17. From the docs:
React version: 18
Steps To Reproduce
Link to code example: https://codesandbox.io/p/sandbox/zealous-thompson-htqfst?file=%2Fsrc%2FApp.js%3A16%2C1
The current behavior
It is not ignoring the additional call to setState. In the example above, the result is 2.
The expected behavior
It should ignore the additional call to setState. In the example above, the result should be 1.
The text was updated successfully, but these errors were encountered: