Skip to content

Commit a9ad64c

Browse files
authored
[DevTools] Stop mounting empty roots (facebook#34467)
1 parent 7fc888d commit a9ad64c

File tree

2 files changed

+24
-26
lines changed

2 files changed

+24
-26
lines changed

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

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2489,7 +2489,7 @@ describe('Store', () => {
24892489
withErrorsOrWarningsIgnored(['test-only:'], async () => {
24902490
await act(() => render(<React.Fragment />));
24912491
});
2492-
expect(store).toMatchInlineSnapshot(`[root]`);
2492+
expect(store).toMatchInlineSnapshot(``);
24932493
expect(store.componentWithErrorCount).toBe(0);
24942494
expect(store.componentWithWarningCount).toBe(0);
24952495
});
@@ -3083,6 +3083,9 @@ describe('Store', () => {
30833083

30843084
it('should handle an empty root', async () => {
30853085
await actAsync(() => render(null));
3086+
expect(store).toMatchInlineSnapshot(``);
3087+
3088+
await actAsync(() => render(<span />));
30863089
expect(store).toMatchInlineSnapshot(`[root]`);
30873090
});
30883091
});

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

Lines changed: 20 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -5322,12 +5322,12 @@ export function attach(
53225322
root: FiberRoot,
53235323
priorityLevel: void | number,
53245324
) {
5325-
const current = root.current;
5325+
const nextFiber = root.current;
53265326
53275327
let prevFiber: null | Fiber = null;
53285328
let rootInstance = rootToFiberInstanceMap.get(root);
53295329
if (!rootInstance) {
5330-
rootInstance = createFiberInstance(current);
5330+
rootInstance = createFiberInstance(nextFiber);
53315331
rootToFiberInstanceMap.set(root, rootInstance);
53325332
idToDevToolsInstanceMap.set(rootInstance.id, rootInstance);
53335333
} else {
@@ -5366,30 +5366,25 @@ export function attach(
53665366
};
53675367
}
53685368
5369-
if (prevFiber !== null) {
5370-
// TODO: relying on this seems a bit fishy.
5371-
const wasMounted =
5372-
prevFiber.memoizedState != null &&
5373-
prevFiber.memoizedState.element != null;
5374-
const isMounted =
5375-
current.memoizedState != null && current.memoizedState.element != null;
5376-
if (!wasMounted && isMounted) {
5377-
// Mount a new root.
5378-
setRootPseudoKey(currentRoot.id, current);
5379-
mountFiberRecursively(current, false);
5380-
} else if (wasMounted && isMounted) {
5381-
// Update an existing root.
5382-
updateFiberRecursively(rootInstance, current, prevFiber, false);
5383-
} else if (wasMounted && !isMounted) {
5384-
// Unmount an existing root.
5385-
unmountInstanceRecursively(rootInstance);
5386-
removeRootPseudoKey(currentRoot.id);
5387-
rootToFiberInstanceMap.delete(root);
5388-
}
5389-
} else {
5369+
const nextIsMounted = nextFiber.child !== null;
5370+
const prevWasMounted = prevFiber !== null && prevFiber.child !== null;
5371+
if (!prevWasMounted && nextIsMounted) {
53905372
// Mount a new root.
5391-
setRootPseudoKey(currentRoot.id, current);
5392-
mountFiberRecursively(current, false);
5373+
setRootPseudoKey(currentRoot.id, nextFiber);
5374+
mountFiberRecursively(nextFiber, false);
5375+
} else if (prevWasMounted && nextIsMounted) {
5376+
if (prevFiber === null) {
5377+
throw new Error(
5378+
'Expected a previous Fiber when updating an existing root.',
5379+
);
5380+
}
5381+
// Update an existing root.
5382+
updateFiberRecursively(rootInstance, nextFiber, prevFiber, false);
5383+
} else if (prevWasMounted && !nextIsMounted) {
5384+
// Unmount an existing root.
5385+
unmountInstanceRecursively(rootInstance);
5386+
removeRootPseudoKey(currentRoot.id);
5387+
rootToFiberInstanceMap.delete(root);
53935388
}
53945389
53955390
if (isProfiling && isProfilingSupported) {

0 commit comments

Comments
 (0)