-
Notifications
You must be signed in to change notification settings - Fork 46.9k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add warning if return pointer is inconsistent (#20219)
Bugs caused by inconsistent return pointers are tricky to diagnose because the source of the error is often in a different part of the codebase from the actual mistake. For example, you might forget to set a return pointer during the render phase, which later causes a crash in the commit phase. This adds a dev-only invariant to the commit phase to check for inconsistencies. With this in place, we'll hopefully catch return pointer errors quickly during local development, when we have the most context for what might have caused it.
- Loading branch information
Showing
3 changed files
with
93 additions
and
0 deletions.
There are no files selected for viewing
61 changes: 61 additions & 0 deletions
61
packages/react-dom/src/__tests__/ReactWrongReturnPointer-test.js
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 |
---|---|---|
@@ -0,0 +1,61 @@ | ||
/** | ||
* Copyright (c) Facebook, Inc. and its affiliates. | ||
* | ||
* This source code is licensed under the MIT license found in the | ||
* LICENSE file in the root directory of this source tree. | ||
* | ||
* @emails react-core | ||
*/ | ||
|
||
let React; | ||
let ReactNoop; | ||
|
||
beforeEach(() => { | ||
React = require('react'); | ||
ReactNoop = require('react-noop-renderer'); | ||
}); | ||
|
||
// Don't feel too guilty if you have to delete this test. | ||
// @gate new | ||
// @gate __DEV__ | ||
test('warns in DEV if return pointer is inconsistent', async () => { | ||
const {useRef, useLayoutEffect} = React; | ||
|
||
let ref = null; | ||
function App({text}) { | ||
ref = useRef(null); | ||
return ( | ||
<> | ||
<Sibling text={text} /> | ||
<div ref={ref}>{text}</div> | ||
</> | ||
); | ||
} | ||
|
||
function Sibling({text}) { | ||
useLayoutEffect(() => { | ||
if (text === 'B') { | ||
// Mutate the return pointer of the div to point to the wrong alternate. | ||
// This simulates the most common type of return pointer inconsistency. | ||
const current = ref.current.fiber; | ||
const workInProgress = current.alternate; | ||
workInProgress.return = current.return; | ||
} | ||
}, [text]); | ||
return null; | ||
} | ||
|
||
const root = ReactNoop.createRoot(); | ||
await ReactNoop.act(async () => { | ||
root.render(<App text="A" />); | ||
}); | ||
|
||
spyOnDev(console, 'error'); | ||
await ReactNoop.act(async () => { | ||
root.render(<App text="B" />); | ||
}); | ||
expect(console.error.calls.count()).toBe(1); | ||
expect(console.error.calls.argsFor(0)[0]).toMatch( | ||
'Internal React error: Return pointer is inconsistent with parent.', | ||
); | ||
}); |
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