-
Notifications
You must be signed in to change notification settings - Fork 47k
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
Bug: StrictMode is not preventing side effects #28898
Comments
const {propA, propB, rest} = useMemo(() => {
const {propA, propB, ...rest} = propsC;
return {propA, propB, rest};
}, [propC]);
useEffect(() => {
// some effect not expected executed on every render
}, [rest]);
// ^^ rest is "stable" Keep in mind that
I think this would be interesting to try out in an experiment. Could be a PR for starters. At a glance, seems reasonable to expect effect dependencies to be stable between double render. You'd also expect the committed HTML to be stable. |
Not really a problem if React from time to time resets one or another branch. It is a problem if such a reset happens on every render.
This could be a little extension to the definition of a It is very important to have this check at the lowest level (React) as optimisations in other places may only temporarily hide the problem. See reduxjs/react-redux#2160 as an example |
Just to keep you in touch - did a dirty spike around the problem and found two models applicable to a given task Solution 1 - "inside"
A simple solution was able to reveal a few cases where we used It also resulted in some noise from direct refs operation in hooks like The downsides of my solution are:
Solution 2 - "outside"The second solution does not require any modification of React except overriding
Expectations:
The combination of such "small cycle" and "large cycle" has proven to be extremely effective without any need to Forget things. |
This issue has been automatically marked as stale. If this issue is still affecting you, please leave any comment (for example, "bump"), and we'll keep it open. We are sorry that we haven't been able to prioritize it yet. If you have any new additional information, please include it with your comment! |
React Compiler removes the majority of "trashing" effects and eslint is pointing to problems. However this approach is still the simplest measure to find a problem in the code that needs fixing. Simple and powerful. If Strict Mode is still around, then this feature should be a part of it. |
React version: any with StrictMode
Steps To Reproduce
useEffect
with unstable dependencyStrictMode
is not able to help hereThe current behavior
https://react.dev/reference/react/StrictMode
I personally never found this behaviour any helpful. It never helped me find a bug, especially a bug related to useEffect.
The expected behavior
I would assume that
StrictMode
should not try to executeuseEffect
twice - it should render Component twice and ensure nouseEffect
oruseMemo
is invalidated.Ideally, it should cause full application re-render to detect memoization issues spanning across multiple components, like using
children
in effect dependencies or passing unstable prop to a component withuseEffect
as such case cannot be detected by isolated re-render.React Forget is going to change the game rules and automatically fix the problems from above, but how one can prove it without having a corresponding testing functionality one can trust?
Unfortunately, this is something very hard to implement in the user space, simultaneously something causing incidents (performance as well as reliability) on a weekly basic
The text was updated successfully, but these errors were encountered: