@@ -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