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