[Fiber] getNearestMountedFiber should consider fibers with alternates as mounted (#35578)
Sebastian Markbåge committed
Jan 21, 2026 at 08:33 UTC
b546603bcb309a52343fd6a7b8751145205f8ac1
1 file changed
+17
-18
packages/react-reconciler/src/ReactFiberTreeReflection.js
+17
-18
@@ -33,25 +33,24 @@ import {NoFlags, Placement, Hydrating} from './ReactFiberFlags';
33
export function getNearestMountedFiber(fiber: Fiber): null | Fiber {
34
let node = fiber;
35
let nearestMounted: null | Fiber = fiber;
36
- if (!fiber.alternate) {
37
- // If there is no alternate, this might be a new tree that isn't inserted
38
- // yet. If it is, then it will have a pending insertion effect on it.
39
- let nextNode: Fiber = node;
40
- do {
41
- node = nextNode;
42
- if ((node.flags & (Placement | Hydrating)) !== NoFlags) {
43
- // This is an insertion or in-progress hydration. The nearest possible
44
- // mounted fiber is the parent but we need to continue to figure out
45
- // if that one is still mounted.
46
- nearestMounted = node.return;
47
- }
48
- // $FlowFixMe[incompatible-type] we bail out when we get a null
49
- nextNode = node.return;
50
- } while (nextNode);
51
- } else {
52
- while (node.return) {
53
- node = node.return;
36
+ // If there is no alternate, this might be a new tree that isn't inserted
37
+ // yet. If it is, then it will have a pending insertion effect on it.
38
+ let nextNode: Fiber = node;
39
+ while (nextNode && !nextNode.alternate) {
40
+ node = nextNode;
41
+ if ((node.flags & (Placement | Hydrating)) !== NoFlags) {
42
+ // This is an insertion or in-progress hydration. The nearest possible
43
+ // mounted fiber is the parent but we need to continue to figure out
44
+ // if that one is still mounted.
45
+ nearestMounted = node.return;
46
}
47
+ // $FlowFixMe[incompatible-type] we bail out when we get a null
48
+ nextNode = node.return;
49
+ }
50
+ // After we've reached an alternate, go the rest of the way to see if the
51
+ // tree is still mounted. If it's not, its return pointer will be disconnected.
52
+ while (node.return) {
53
+ node = node.return;
54
}
55
if (node.tag === HostRoot) {
56
// TODO: Check if this was a nested HostRoot when used with