-
Notifications
You must be signed in to change notification settings - Fork 48.6k
Bug: state is not preverved across re-mounts in StrictMode #25893
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
Comments
I simplified a lot the reproduction scenario, but for archival purposes here is the original: https://stackblitz.com/edit/react-strict-mode-uvjw3r?file=index.js (newer simpler one is here: https://stackblitz.com/edit/react-strict-mode-cosrnr?file=index.js,index.html) |
This issue is already fixed in #25583 You can try it out by setting dependency in your project to reason I think why it didn't work in 18.2It is not about state actually, it is about the renderWithHooks() is called twice, with the same In your code, the existence of
|
We don't consider this a bug. Your component's rendering function reads If you want to store information from previous renders, do not use refs for this. Use state. Here's how. It looks like we "accidentally" fixed this on |
Thanks, that's interesting, it makes me rethink a lot of the ways I used |
This example doesn't read or write refs during rendering, so it's OK. |
Uh oh!
There was an error while loading. Please reload this page.
State is not preverved across StrictMode re-mounts. If StrictMode is removed there is no bug.
React version: 18.2.0
Steps To Reproduce
boolean
state in theuseSetStateWithoutUseEffect
hook is set totrue
(we have aconsole.log('setting boolean to true')
.However it is instantly set back to
false
, even though its setter is not called...I have no idea what causes this, but since it only happens in strict mode, and reflects a loss of state (and I can't for the life of me see any bug in the hook
useSetStateWithoutUseEffect
, as I think setting state conditionnally outside a useEffect or useCallback shouldn't be a problem), I can only assume it's a React bug.Here is the hook code:
Link to code example: https://stackblitz.com/edit/react-strict-mode-cosrnr?file=index.js,index.html
The current behavior
The value
boolean
is set totrue
as expected on the click, but then instantaneously set back tofalse
, even though its setter is not called...The expected behavior
The value
boolean
should be set to true upon clicking and stay that way. (which works in non-strict mode)EDIT: simplified a LOT the code example
The text was updated successfully, but these errors were encountered: