Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
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
fix: don't run effects if a render phase update results in unchanged deps #20676
fix: don't run effects if a render phase update results in unchanged deps #20676
Changes from 8 commits
c8435e7
5bcce83
6cfd74d
9dfcd56
0f58f40
3cdd353
e39a670
77ed727
5c24bb5
File filter
Filter by extension
Conversations
Jump to
There are no files selected for viewing
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
When would the return values for
isEffect(prevMemoizedState)
andisEffect(nextMemoizedState)
ever differ? Can we just checkisEffect(nextMemoizedState)
here?There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think so. But I haven't checked if Effect hooks start with
memoizedState = null
. I'll see if can improve the type story here.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
If they did, that wouldn't matter here because we early return in
didHooksChange
if either one was null.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Let's just do this tweak in a follow up? :) Would be nice.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Not a fan of this new coupling. Especially since this will not work as expected for e.g.
useState({ tag, create, destroy, deps, next });
even though that's unlikely to occur in userland. @bvaughn Any suggestions how this should work now thathook.memoizedState
always changes even if deps didn't?There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
DevTools probably should be resilient to this happening, however, if you want to kick the can down the road, another solution could be to assign
hook.memoizedState = currentHook.memoizedState
when you bailout. Instead of the result ofpushEffect
. In the case of of a normal update, it's a no-op because that's what it already points to. But for render phase updates, it will restore the original object.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
If you end up doing this, please leave a TODO here so we remember this is accidentally coupled
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This was actually my first intuition but I didn't really grasp how
hook
relates tocurrentHook
.But since you seem to be ok with the current approach I'm keeping the initial fix. I added an additional type check to
memoizedState.deps
in case someone hasuseState({ deps: 1, tag, create, destroy, next });
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Still planning on looking at this question. It's next in my queue.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
At first I thought this might break the Profiler too (how it determines what rendered and what didn't) but I forgot that uses the special
PerformedWork
tag.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This doesn't seem like the correct approach. I would rather restore the memoized state when we trigger a render phase update.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I tried
locally which makes more sense conceptually. However, this results in a duplicate warning in
react/packages/react-reconciler/src/__tests__/ReactHooks-test.internal.js
Lines 614 to 622 in d17086c
passes all tests locally but seems even more sketchy.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The fix you committed is more correct. We should compare against the current hook, not the hook from the pass where the render phase update happened.
The bug is that in the case of a render phase update, we're not persisting the most current hook. We're persisting the "intermediate" hook that happens before the render phase update replaces it.
Is there a reason you don't like your fix that I'm overlooking?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The failed devtools test prompted my response. Right now we're invalidating the memoizedState even though the deps didn't change. I suspected that this may cause performance regressions in other places considering that everywhere we compare
hook.memoizedState === prevHook.memoizedState
it'll now evaluate to false even though the deps didn't change.But maybe memoizedState was never intended to be used that way. It might make sense to compare the actual deps not just memoizedState in devtools.
I'll look into the test now. Thanks for the clarification 👍