Skip to content

Commit

Permalink
DevTools: Support an element mounting before its owner (#21562)
Browse files Browse the repository at this point in the history
  • Loading branch information
Brian Vaughn authored May 25, 2021
1 parent 2d8d133 commit 965fb8b
Show file tree
Hide file tree
Showing 2 changed files with 46 additions and 1 deletion.
39 changes: 39 additions & 0 deletions packages/react-devtools-shared/src/__tests__/store-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,45 @@ describe('Store', () => {
expect(store).toMatchSnapshot('2: add host nodes');
});

// This test is not the same cause as what's reported on GitHub,
// but the resulting behavior (owner mounting after descendant) is the same.
// Thec ase below is admittedly contrived and relies on side effects.
// I'mnot yet sure of how to reduce the GitHub reported production case to a test though.
// See https://github.com/facebook/react/issues/21445
it('should handle when a component mounts before its owner', () => {
const promise = new Promise(resolve => {});

let Dynamic = null;
const Owner = () => {
Dynamic = <Child />;
throw promise;
};
const Parent = () => {
return Dynamic;
};
const Child = () => null;

const container = document.createElement('div');

act(() =>
ReactDOM.render(
<>
<React.Suspense fallback="Loading...">
<Owner />
</React.Suspense>
<Parent />
</>,
container,
),
);
expect(store).toMatchInlineSnapshot(`
[root]
<Suspense>
▾ <Parent>
<Child>
`);
});

describe('collapseNodesByDefault:false', () => {
beforeEach(() => {
store.collapseNodesByDefault = false;
Expand Down
8 changes: 7 additions & 1 deletion packages/react-devtools-shared/src/backend/renderer.js
Original file line number Diff line number Diff line change
Expand Up @@ -1753,7 +1753,13 @@ export function attach(
const elementType = getElementTypeForFiber(fiber);
const {_debugOwner} = fiber;

const ownerID = _debugOwner != null ? getFiberIDThrows(_debugOwner) : 0;
// Ideally we should call getFiberIDThrows() for _debugOwner,
// since owners are almost always higher in the tree (and so have already been processed),
// but in some (rare) instances reported in open source, a descendant mounts before an owner.
// Since this is a DEV only field it's probably okay to also just lazily generate and ID here if needed.
// See https://github.com/facebook/react/issues/21445
const ownerID =
_debugOwner != null ? getOrGenerateFiberID(_debugOwner) : 0;
const parentID = parentFiber ? getFiberIDThrows(parentFiber) : 0;

const displayNameStringID = getStringID(displayName);
Expand Down

0 comments on commit 965fb8b

Please sign in to comment.