Skip to content

Commit fcb339d

Browse files
author
Brian Vaughn
committed
DevTools: Support an element mounting before its owner
1 parent a731a51 commit fcb339d

File tree

2 files changed

+42
-1
lines changed

2 files changed

+42
-1
lines changed

packages/react-devtools-shared/src/__tests__/store-test.js

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -60,6 +60,43 @@ describe('Store', () => {
6060
expect(store).toMatchSnapshot('2: add host nodes');
6161
});
6262

63+
// This is a weird and admittedly contrived edge case.
64+
// It's not the same cause as what's reported on GitHub,
65+
// but the resulting behavior (owner mounting after descendant) is the same.
66+
// See https://github.com/facebook/react/issues/21445
67+
it('should handle when a component mounts before its owner', () => {
68+
const promise = Promise.resolve();
69+
70+
let Dynamic = null;
71+
const Owner = () => {
72+
Dynamic = <Child />;
73+
throw promise;
74+
return null;
75+
};
76+
const Parent = () => {
77+
return Dynamic;
78+
};
79+
const Child = () => null;
80+
81+
const container = document.createElement('div');
82+
83+
act(() => ReactDOM.render(
84+
<>
85+
<React.Suspense fallback="Loading...">
86+
<Owner />
87+
</React.Suspense>
88+
<Parent />
89+
</>,
90+
container,
91+
));
92+
expect(store).toMatchInlineSnapshot(`
93+
[root]
94+
<Suspense>
95+
▾ <Parent>
96+
<Child>
97+
`);
98+
});
99+
63100
describe('collapseNodesByDefault:false', () => {
64101
beforeEach(() => {
65102
store.collapseNodesByDefault = false;

packages/react-devtools-shared/src/backend/renderer.js

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1753,7 +1753,11 @@ export function attach(
17531753
const elementType = getElementTypeForFiber(fiber);
17541754
const {_debugOwner} = fiber;
17551755

1756-
const ownerID = _debugOwner != null ? getFiberIDThrows(_debugOwner) : 0;
1756+
// HACK Ideally we would call getFiberIDThrows() for _debugOwner,
1757+
// but in some edge cases the owner doesn't yet have an ID generated.
1758+
// Since this is a DEV only field it's probably okay to also just generate if needed here.
1759+
// See https://github.com/facebook/react/issues/21445
1760+
const ownerID = _debugOwner != null ? getOrGenerateFiberID(_debugOwner) : 0;
17571761
const parentID = parentFiber ? getFiberIDThrows(parentFiber) : 0;
17581762

17591763
const displayNameStringID = getStringID(displayName);

0 commit comments

Comments
 (0)