Skip to content

Commit 436bee1

Browse files
committed
[DevTools] Stop mounting empty roots
1 parent c9602e9 commit 436bee1

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
@@ -5355,12 +5355,12 @@ export function attach(
53555355
root: FiberRoot,
53565356
priorityLevel: void | number,
53575357
) {
5358-
const current = root.current;
5358+
const nextFiber = root.current;
53595359
53605360
let prevFiber: null | Fiber = null;
53615361
let rootInstance = rootToFiberInstanceMap.get(root);
53625362
if (!rootInstance) {
5363-
rootInstance = createFiberInstance(current);
5363+
rootInstance = createFiberInstance(nextFiber);
53645364
rootToFiberInstanceMap.set(root, rootInstance);
53655365
idToDevToolsInstanceMap.set(rootInstance.id, rootInstance);
53665366
} else {
@@ -5399,30 +5399,25 @@ export function attach(
53995399
};
54005400
}
54015401
5402-
if (prevFiber !== null) {
5403-
// TODO: relying on this seems a bit fishy.
5404-
const wasMounted =
5405-
prevFiber.memoizedState != null &&
5406-
prevFiber.memoizedState.element != null;
5407-
const isMounted =
5408-
current.memoizedState != null && current.memoizedState.element != null;
5409-
if (!wasMounted && isMounted) {
5410-
// Mount a new root.
5411-
setRootPseudoKey(currentRoot.id, current);
5412-
mountFiberRecursively(current, false);
5413-
} else if (wasMounted && isMounted) {
5414-
// Update an existing root.
5415-
updateFiberRecursively(rootInstance, current, prevFiber, false);
5416-
} else if (wasMounted && !isMounted) {
5417-
// Unmount an existing root.
5418-
unmountInstanceRecursively(rootInstance);
5419-
removeRootPseudoKey(currentRoot.id);
5420-
rootToFiberInstanceMap.delete(root);
5421-
}
5422-
} else {
5402+
const isMounted = nextFiber.child !== null;
5403+
const wasMounted = prevFiber !== null && prevFiber.child !== null;
5404+
if (!wasMounted && isMounted) {
54235405
// Mount a new root.
5424-
setRootPseudoKey(currentRoot.id, current);
5425-
mountFiberRecursively(current, false);
5406+
setRootPseudoKey(currentRoot.id, nextFiber);
5407+
mountFiberRecursively(nextFiber, false);
5408+
} else if (wasMounted && isMounted) {
5409+
if (prevFiber === null) {
5410+
throw new Error(
5411+
'Expected previous fiber when updating an existing root',
5412+
);
5413+
}
5414+
// Update an existing root.
5415+
updateFiberRecursively(rootInstance, nextFiber, prevFiber, false);
5416+
} else if (wasMounted && !isMounted) {
5417+
// Unmount an existing root.
5418+
unmountInstanceRecursively(rootInstance);
5419+
removeRootPseudoKey(currentRoot.id);
5420+
rootToFiberInstanceMap.delete(root);
54265421
}
54275422
54285423
if (isProfiling && isProfilingSupported) {

0 commit comments

Comments
 (0)