-
Notifications
You must be signed in to change notification settings - Fork 46.5k
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
[React 19]useEffect cleaned the wrong state in StrictMode #31098
Comments
This issue seems interesting! I would love to contribute or discuss further. 👍 |
This behavior is likely due to how React handles state and component updates in the given version. It seems like React is treating the useState call as if it is creating a new state object each time the component re-renders, which should not happen in a normal scenario. The behavior can be linked to potential issues or changes in state handling, particularly in versions around [email protected] as mentioned in the screenshot。 Solution and Recommendation: Verify React Version and Known Issues: Check the React version you are using and look for related issues in the React repository. The issue link (#29634) mentioned in the screenshot might provide more context. Modified Code Example (If Needed): import { useEffect, useRef } from "react"; let stateId = 0; useEffect(() => { console.log("render state", stateRef.current); state: {stateRef.current.stateId} ;} Key Changes: Replaced useState with useRef to maintain a stable reference to the state object. This ensures that the destroy method is only called when the component is unmounted, avoiding unexpected destruction of the state object. By understanding the root cause and applying the appropriate fix, the component can behave predictably across different React versions. |
Summary
https://codesandbox.io/p/sandbox/youthful-orla-lk7g89
console output:
Final render:
state: 1
, the destroyed state[email protected] behavior
related issue: #29634
Final render:
state: 2
, the destroyed stateThe text was updated successfully, but these errors were encountered: