-
Notifications
You must be signed in to change notification settings - Fork 48.7k
Don't warn about unmounted updates if pending passive unmount #18096
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
Merged
bvaughn
merged 1 commit into
facebook:master
from
bvaughn:suppress-unmounted-state-warning-pending-passive-effects
Feb 21, 2020
Merged
Changes from all commits
Commits
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 hidden or 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 hidden or 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 |
---|---|---|
|
@@ -1144,6 +1144,184 @@ function loadModules({ | |
]); | ||
}); | ||
}); | ||
|
||
it('does not warn about state updates for unmounted components with pending passive unmounts', () => { | ||
let completePendingRequest = null; | ||
function Component() { | ||
Scheduler.unstable_yieldValue('Component'); | ||
const [didLoad, setDidLoad] = React.useState(false); | ||
React.useLayoutEffect(() => { | ||
Scheduler.unstable_yieldValue('layout create'); | ||
return () => { | ||
Scheduler.unstable_yieldValue('layout destroy'); | ||
}; | ||
}, []); | ||
React.useEffect(() => { | ||
Scheduler.unstable_yieldValue('passive create'); | ||
// Mimic an XHR request with a complete handler that updates state. | ||
completePendingRequest = () => setDidLoad(true); | ||
return () => { | ||
Scheduler.unstable_yieldValue('passive destroy'); | ||
}; | ||
}, []); | ||
return didLoad; | ||
} | ||
|
||
act(() => { | ||
ReactNoop.renderToRootWithID(<Component />, 'root', () => | ||
Scheduler.unstable_yieldValue('Sync effect'), | ||
); | ||
expect(Scheduler).toFlushAndYieldThrough([ | ||
'Component', | ||
'layout create', | ||
'Sync effect', | ||
]); | ||
ReactNoop.flushPassiveEffects(); | ||
expect(Scheduler).toHaveYielded(['passive create']); | ||
|
||
// Unmount but don't process pending passive destroy function | ||
ReactNoop.unmountRootWithID('root'); | ||
expect(Scheduler).toFlushAndYieldThrough(['layout destroy']); | ||
|
||
// Simulate an XHR completing, which will cause a state update- | ||
// but should not log a warning. | ||
completePendingRequest(); | ||
|
||
ReactNoop.flushPassiveEffects(); | ||
expect(Scheduler).toHaveYielded(['passive destroy']); | ||
}); | ||
}); | ||
|
||
it('still warns about state updates for unmounted components with no pending passive unmounts', () => { | ||
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. Can you add a test for when there's a pending unmount on a different component? 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. Sure! |
||
let completePendingRequest = null; | ||
function Component() { | ||
Scheduler.unstable_yieldValue('Component'); | ||
const [didLoad, setDidLoad] = React.useState(false); | ||
React.useLayoutEffect(() => { | ||
Scheduler.unstable_yieldValue('layout create'); | ||
// Mimic an XHR request with a complete handler that updates state. | ||
completePendingRequest = () => setDidLoad(true); | ||
return () => { | ||
Scheduler.unstable_yieldValue('layout destroy'); | ||
}; | ||
}, []); | ||
return didLoad; | ||
} | ||
|
||
act(() => { | ||
ReactNoop.renderToRootWithID(<Component />, 'root', () => | ||
Scheduler.unstable_yieldValue('Sync effect'), | ||
); | ||
expect(Scheduler).toFlushAndYieldThrough([ | ||
'Component', | ||
'layout create', | ||
'Sync effect', | ||
]); | ||
|
||
// Unmount but don't process pending passive destroy function | ||
ReactNoop.unmountRootWithID('root'); | ||
expect(Scheduler).toFlushAndYieldThrough(['layout destroy']); | ||
|
||
// Simulate an XHR completing. | ||
expect(completePendingRequest).toErrorDev( | ||
"Warning: Can't perform a React state update on an unmounted component.", | ||
); | ||
}); | ||
}); | ||
|
||
it('still warns if there are pending passive unmount effects but not for the current fiber', () => { | ||
let completePendingRequest = null; | ||
function ComponentWithXHR() { | ||
Scheduler.unstable_yieldValue('Component'); | ||
const [didLoad, setDidLoad] = React.useState(false); | ||
React.useLayoutEffect(() => { | ||
Scheduler.unstable_yieldValue('a:layout create'); | ||
return () => { | ||
Scheduler.unstable_yieldValue('a:layout destroy'); | ||
}; | ||
}, []); | ||
React.useEffect(() => { | ||
Scheduler.unstable_yieldValue('a:passive create'); | ||
// Mimic an XHR request with a complete handler that updates state. | ||
completePendingRequest = () => setDidLoad(true); | ||
}, []); | ||
return didLoad; | ||
} | ||
|
||
function ComponentWithPendingPassiveUnmount() { | ||
React.useEffect(() => { | ||
Scheduler.unstable_yieldValue('b:passive create'); | ||
return () => { | ||
Scheduler.unstable_yieldValue('b:passive destroy'); | ||
}; | ||
}, []); | ||
return null; | ||
} | ||
|
||
act(() => { | ||
ReactNoop.renderToRootWithID( | ||
<> | ||
<ComponentWithXHR /> | ||
<ComponentWithPendingPassiveUnmount /> | ||
</>, | ||
'root', | ||
() => Scheduler.unstable_yieldValue('Sync effect'), | ||
); | ||
expect(Scheduler).toFlushAndYieldThrough([ | ||
'Component', | ||
'a:layout create', | ||
'Sync effect', | ||
]); | ||
ReactNoop.flushPassiveEffects(); | ||
expect(Scheduler).toHaveYielded([ | ||
'a:passive create', | ||
'b:passive create', | ||
]); | ||
|
||
// Unmount but don't process pending passive destroy function | ||
ReactNoop.unmountRootWithID('root'); | ||
expect(Scheduler).toFlushAndYieldThrough(['a:layout destroy']); | ||
|
||
// Simulate an XHR completing in the component without a pending passive effect.. | ||
expect(completePendingRequest).toErrorDev( | ||
"Warning: Can't perform a React state update on an unmounted component.", | ||
); | ||
}); | ||
}); | ||
|
||
it('still warns about state updates from within passive unmount function', () => { | ||
function Component() { | ||
Scheduler.unstable_yieldValue('Component'); | ||
const [didLoad, setDidLoad] = React.useState(false); | ||
React.useEffect(() => { | ||
Scheduler.unstable_yieldValue('passive create'); | ||
return () => { | ||
setDidLoad(true); | ||
Scheduler.unstable_yieldValue('passive destroy'); | ||
}; | ||
}, []); | ||
return didLoad; | ||
} | ||
|
||
act(() => { | ||
ReactNoop.renderToRootWithID(<Component />, 'root', () => | ||
Scheduler.unstable_yieldValue('Sync effect'), | ||
); | ||
expect(Scheduler).toFlushAndYieldThrough([ | ||
'Component', | ||
'Sync effect', | ||
'passive create', | ||
]); | ||
|
||
// Unmount but don't process pending passive destroy function | ||
ReactNoop.unmountRootWithID('root'); | ||
expect(() => { | ||
expect(Scheduler).toFlushAndYield(['passive destroy']); | ||
}).toErrorDev( | ||
"Warning: Can't perform a React state update on an unmounted component.", | ||
); | ||
}); | ||
}); | ||
} | ||
|
||
it('updates have async priority', () => { | ||
|
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.
odd, syntax highlighting seems to be broken for this file

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.
Huh. Yeah for me me too.