Bug: Setting same state in useLayoutEffect triggers additional renders in React 18 #25511
Labels
Resolution: Stale
Automatically closed due to inactivity
Status: Unconfirmed
A potential issue that we haven't yet confirmed as a bug
I'm not sure if this is a bug or expected behaviour, but we've noticed this change while upgrading our project to React 18. Setting state in a
useLayoutEffect
in this particular scenario is causing additional renders in React 18 compared to React 17. The previous behaviour in React 17 seems like the correct, expected behaviour.React version: 18.2.0
Steps To Reproduce
Given this component:
Note that this is a contrived example to minimally demonstrate the issue; it ignores proper effect cleanup, etc.
Link to codesandbox: https://codesandbox.io/s/flamboyant-mendel-3gxysj?file=/src/index.js
The current behavior
React 18 logs this output:
The expected behavior
React 17 logs this output:
Notes:
setState("loading")
is causing the extra render. Shouldn't this call have no effect since the state's initial value is already "loading"? If you change the state's initial value to something else (likeundefined
), then the output is what I'd expect:useEffect
produces the expected output in both React 17 and React 18.The text was updated successfully, but these errors were encountered: