mirror of
https://github.com/zebrajr/react.git
synced 2025-12-06 12:20:20 +01:00
[DevTools] Stop mounting empty roots (#34467)
This commit is contained in:
parent
7fc888dde2
commit
a9ad64c852
|
|
@ -2489,7 +2489,7 @@ describe('Store', () => {
|
||||||
withErrorsOrWarningsIgnored(['test-only:'], async () => {
|
withErrorsOrWarningsIgnored(['test-only:'], async () => {
|
||||||
await act(() => render(<React.Fragment />));
|
await act(() => render(<React.Fragment />));
|
||||||
});
|
});
|
||||||
expect(store).toMatchInlineSnapshot(`[root]`);
|
expect(store).toMatchInlineSnapshot(``);
|
||||||
expect(store.componentWithErrorCount).toBe(0);
|
expect(store.componentWithErrorCount).toBe(0);
|
||||||
expect(store.componentWithWarningCount).toBe(0);
|
expect(store.componentWithWarningCount).toBe(0);
|
||||||
});
|
});
|
||||||
|
|
@ -3083,6 +3083,9 @@ describe('Store', () => {
|
||||||
|
|
||||||
it('should handle an empty root', async () => {
|
it('should handle an empty root', async () => {
|
||||||
await actAsync(() => render(null));
|
await actAsync(() => render(null));
|
||||||
|
expect(store).toMatchInlineSnapshot(``);
|
||||||
|
|
||||||
|
await actAsync(() => render(<span />));
|
||||||
expect(store).toMatchInlineSnapshot(`[root]`);
|
expect(store).toMatchInlineSnapshot(`[root]`);
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
|
|
|
||||||
|
|
@ -5322,12 +5322,12 @@ export function attach(
|
||||||
root: FiberRoot,
|
root: FiberRoot,
|
||||||
priorityLevel: void | number,
|
priorityLevel: void | number,
|
||||||
) {
|
) {
|
||||||
const current = root.current;
|
const nextFiber = root.current;
|
||||||
|
|
||||||
let prevFiber: null | Fiber = null;
|
let prevFiber: null | Fiber = null;
|
||||||
let rootInstance = rootToFiberInstanceMap.get(root);
|
let rootInstance = rootToFiberInstanceMap.get(root);
|
||||||
if (!rootInstance) {
|
if (!rootInstance) {
|
||||||
rootInstance = createFiberInstance(current);
|
rootInstance = createFiberInstance(nextFiber);
|
||||||
rootToFiberInstanceMap.set(root, rootInstance);
|
rootToFiberInstanceMap.set(root, rootInstance);
|
||||||
idToDevToolsInstanceMap.set(rootInstance.id, rootInstance);
|
idToDevToolsInstanceMap.set(rootInstance.id, rootInstance);
|
||||||
} else {
|
} else {
|
||||||
|
|
@ -5366,30 +5366,25 @@ export function attach(
|
||||||
};
|
};
|
||||||
}
|
}
|
||||||
|
|
||||||
if (prevFiber !== null) {
|
const nextIsMounted = nextFiber.child !== null;
|
||||||
// TODO: relying on this seems a bit fishy.
|
const prevWasMounted = prevFiber !== null && prevFiber.child !== null;
|
||||||
const wasMounted =
|
if (!prevWasMounted && nextIsMounted) {
|
||||||
prevFiber.memoizedState != null &&
|
|
||||||
prevFiber.memoizedState.element != null;
|
|
||||||
const isMounted =
|
|
||||||
current.memoizedState != null && current.memoizedState.element != null;
|
|
||||||
if (!wasMounted && isMounted) {
|
|
||||||
// Mount a new root.
|
|
||||||
setRootPseudoKey(currentRoot.id, current);
|
|
||||||
mountFiberRecursively(current, false);
|
|
||||||
} else if (wasMounted && isMounted) {
|
|
||||||
// Update an existing root.
|
|
||||||
updateFiberRecursively(rootInstance, current, prevFiber, false);
|
|
||||||
} else if (wasMounted && !isMounted) {
|
|
||||||
// Unmount an existing root.
|
|
||||||
unmountInstanceRecursively(rootInstance);
|
|
||||||
removeRootPseudoKey(currentRoot.id);
|
|
||||||
rootToFiberInstanceMap.delete(root);
|
|
||||||
}
|
|
||||||
} else {
|
|
||||||
// Mount a new root.
|
// Mount a new root.
|
||||||
setRootPseudoKey(currentRoot.id, current);
|
setRootPseudoKey(currentRoot.id, nextFiber);
|
||||||
mountFiberRecursively(current, false);
|
mountFiberRecursively(nextFiber, false);
|
||||||
|
} else if (prevWasMounted && nextIsMounted) {
|
||||||
|
if (prevFiber === null) {
|
||||||
|
throw new Error(
|
||||||
|
'Expected a previous Fiber when updating an existing root.',
|
||||||
|
);
|
||||||
|
}
|
||||||
|
// Update an existing root.
|
||||||
|
updateFiberRecursively(rootInstance, nextFiber, prevFiber, false);
|
||||||
|
} else if (prevWasMounted && !nextIsMounted) {
|
||||||
|
// Unmount an existing root.
|
||||||
|
unmountInstanceRecursively(rootInstance);
|
||||||
|
removeRootPseudoKey(currentRoot.id);
|
||||||
|
rootToFiberInstanceMap.delete(root);
|
||||||
}
|
}
|
||||||
|
|
||||||
if (isProfiling && isProfilingSupported) {
|
if (isProfiling && isProfilingSupported) {
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue
Block a user