-
Notifications
You must be signed in to change notification settings - Fork 47.2k
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
Merged
bvaughn
merged 9 commits into
facebook:master
from
eps1lon:fix/gdsfp-effect-alternating
Jan 29, 2021
Merged
Changes from 3 commits
Commits
Show all changes
9 commits
Select commit
Hold shift + click to select a range
c8435e7
current behavior of effect dependencies on render phase updates
eps1lon 5bcce83
fix: don't schedule effects when render phase updates aren't committed
eps1lon 6cfd74d
apply fix to new fork
eps1lon 9dfcd56
Use suggested test name
eps1lon 0f58f40
devtools: compare effects by deps not referential equality
eps1lon 3cdd353
disable type checking for added functions
eps1lon e39a670
bump CircleCI
eps1lon 77ed727
Be more defensive about `useState({ deps })`
eps1lon 5c24bb5
bail out early if memoizedState didn't change
eps1lon File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -3760,4 +3760,47 @@ describe('ReactHooksWithNoopRenderer', () => { | |
// effects would not fire. | ||
expect(Scheduler).toHaveYielded(['Unmount A', 'Unmount B']); | ||
}); | ||
|
||
it('effect dependencies are consistent with yielded values when triggering state updates during render', () => { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Suggested alt name for test: "effect dependencies are persisted after a render phase update" |
||
let handleClick; | ||
function Test() { | ||
const [count, setCount] = useState(0); | ||
|
||
useEffect(() => { | ||
Scheduler.unstable_yieldValue(`Effect: ${count}`); | ||
}, [count]); | ||
|
||
if (count > 0) { | ||
setCount(0); | ||
} | ||
|
||
handleClick = () => setCount(2); | ||
|
||
return <Text text={`Render: ${count}`} />; | ||
} | ||
|
||
ReactNoop.act(() => { | ||
ReactNoop.render(<Test />); | ||
}); | ||
|
||
expect(Scheduler).toHaveYielded(['Render: 0', 'Effect: 0']); | ||
|
||
ReactNoop.act(() => { | ||
handleClick(); | ||
}); | ||
|
||
expect(Scheduler).toHaveYielded(['Render: 0']); | ||
|
||
ReactNoop.act(() => { | ||
handleClick(); | ||
}); | ||
|
||
expect(Scheduler).toHaveYielded(['Render: 0']); | ||
|
||
ReactNoop.act(() => { | ||
handleClick(); | ||
}); | ||
|
||
expect(Scheduler).toHaveYielded(['Render: 0']); | ||
}); | ||
}); |
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.
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 👍