Skip to content

Commit

Permalink
DevTools: Support an element mounting before its owner
Browse files Browse the repository at this point in the history
  • Loading branch information
Brian Vaughn committed May 25, 2021
1 parent a731a51 commit fcb339d
Show file tree
Hide file tree
Showing 2 changed files with 42 additions and 1 deletion.
37 changes: 37 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,43 @@ describe('Store', () => {
expect(store).toMatchSnapshot('2: add host nodes');
});

// This is a weird and admittedly contrived edge case.
// It's not the same cause as what's reported on GitHub,
// but the resulting behavior (owner mounting after descendant) is the same.
// See https://github.com/facebook/react/issues/21445
it('should handle when a component mounts before its owner', () => {
const promise = Promise.resolve();

let Dynamic = null;
const Owner = () => {
Dynamic = <Child />;
throw promise;
return null;
};
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
6 changes: 5 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,11 @@ export function attach(
const elementType = getElementTypeForFiber(fiber);
const {_debugOwner} = fiber;

const ownerID = _debugOwner != null ? getFiberIDThrows(_debugOwner) : 0;
// HACK Ideally we would call getFiberIDThrows() for _debugOwner,
// but in some edge cases the owner doesn't yet have an ID generated.
// Since this is a DEV only field it's probably okay to also just generate if needed here.
// 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 fcb339d

Please sign in to comment.