@@ -162,7 +162,11 @@ const callComponentWillUnmountWithTimer = function(current, instance) {
162
162
} ;
163
163
164
164
// Capture errors so they don't interrupt unmounting.
165
- function safelyCallComponentWillUnmount ( current , instance ) {
165
+ function safelyCallComponentWillUnmount (
166
+ current : Fiber ,
167
+ instance : any ,
168
+ nearestMountedAncestor : Fiber ,
169
+ ) {
166
170
if ( __DEV__ ) {
167
171
invokeGuardedCallback (
168
172
null ,
@@ -173,13 +177,13 @@ function safelyCallComponentWillUnmount(current, instance) {
173
177
) ;
174
178
if ( hasCaughtError ( ) ) {
175
179
const unmountError = clearCaughtError ( ) ;
176
- captureCommitPhaseError ( current , current . return , unmountError ) ;
180
+ captureCommitPhaseError ( current , nearestMountedAncestor , unmountError ) ;
177
181
}
178
182
} else {
179
183
try {
180
184
callComponentWillUnmountWithTimer ( current , instance ) ;
181
185
} catch ( unmountError ) {
182
- captureCommitPhaseError ( current , current . return , unmountError ) ;
186
+ captureCommitPhaseError ( current , nearestMountedAncestor , unmountError ) ;
183
187
}
184
188
}
185
189
}
@@ -974,6 +978,7 @@ function commitDetachRef(current: Fiber) {
974
978
function commitUnmount (
975
979
finishedRoot : FiberRoot ,
976
980
current : Fiber ,
981
+ nearestMountedAncestor : Fiber ,
977
982
renderPriorityLevel : ReactPriorityLevel ,
978
983
) : void {
979
984
onCommitUnmount ( current ) ;
@@ -1001,10 +1006,10 @@ function commitUnmount(
1001
1006
current . mode & ProfileMode
1002
1007
) {
1003
1008
startLayoutEffectTimer ( ) ;
1004
- safelyCallDestroy ( current , current . return , destroy ) ;
1009
+ safelyCallDestroy ( current , nearestMountedAncestor , destroy ) ;
1005
1010
recordLayoutEffectDuration ( current ) ;
1006
1011
} else {
1007
- safelyCallDestroy ( current , current . return , destroy ) ;
1012
+ safelyCallDestroy ( current , nearestMountedAncestor , destroy ) ;
1008
1013
}
1009
1014
}
1010
1015
}
@@ -1018,7 +1023,11 @@ function commitUnmount(
1018
1023
safelyDetachRef ( current ) ;
1019
1024
const instance = current . stateNode ;
1020
1025
if ( typeof instance . componentWillUnmount === 'function' ) {
1021
- safelyCallComponentWillUnmount ( current , instance ) ;
1026
+ safelyCallComponentWillUnmount (
1027
+ current ,
1028
+ instance ,
1029
+ nearestMountedAncestor ,
1030
+ ) ;
1022
1031
}
1023
1032
return ;
1024
1033
}
@@ -1031,7 +1040,12 @@ function commitUnmount(
1031
1040
// We are also not using this parent because
1032
1041
// the portal will get pushed immediately.
1033
1042
if ( supportsMutation ) {
1034
- unmountHostComponents ( finishedRoot , current , renderPriorityLevel ) ;
1043
+ unmountHostComponents (
1044
+ finishedRoot ,
1045
+ current ,
1046
+ nearestMountedAncestor ,
1047
+ renderPriorityLevel ,
1048
+ ) ;
1035
1049
} else if ( supportsPersistence ) {
1036
1050
emptyPortalContainer ( current ) ;
1037
1051
}
@@ -1071,6 +1085,7 @@ function commitUnmount(
1071
1085
function commitNestedUnmounts (
1072
1086
finishedRoot : FiberRoot ,
1073
1087
root : Fiber ,
1088
+ nearestMountedAncestor : Fiber ,
1074
1089
renderPriorityLevel : ReactPriorityLevel ,
1075
1090
) : void {
1076
1091
// While we're inside a removed host node we don't want to call
@@ -1080,7 +1095,12 @@ function commitNestedUnmounts(
1080
1095
// we do an inner loop while we're still inside the host node.
1081
1096
let node : Fiber = root ;
1082
1097
while ( true ) {
1083
- commitUnmount ( finishedRoot , node , renderPriorityLevel ) ;
1098
+ commitUnmount (
1099
+ finishedRoot ,
1100
+ node ,
1101
+ nearestMountedAncestor ,
1102
+ renderPriorityLevel ,
1103
+ ) ;
1084
1104
// Visit children because they may contain more composite or host nodes.
1085
1105
// Skip portals because commitUnmount() currently visits them recursively.
1086
1106
if (
@@ -1361,9 +1381,10 @@ function insertOrAppendPlacementNode(
1361
1381
}
1362
1382
1363
1383
function unmountHostComponents (
1364
- finishedRoot ,
1365
- current ,
1366
- renderPriorityLevel ,
1384
+ finishedRoot : FiberRoot ,
1385
+ current : Fiber ,
1386
+ nearestMountedAncestor : Fiber ,
1387
+ renderPriorityLevel : ReactPriorityLevel ,
1367
1388
) : void {
1368
1389
// We only have the top Fiber that was deleted but we need to recurse down its
1369
1390
// children to find all the terminal nodes.
@@ -1412,7 +1433,12 @@ function unmountHostComponents(
1412
1433
}
1413
1434
1414
1435
if ( node . tag === HostComponent || node . tag === HostText ) {
1415
- commitNestedUnmounts ( finishedRoot , node , renderPriorityLevel ) ;
1436
+ commitNestedUnmounts (
1437
+ finishedRoot ,
1438
+ node ,
1439
+ nearestMountedAncestor ,
1440
+ renderPriorityLevel ,
1441
+ ) ;
1416
1442
// After all the children have unmounted, it is now safe to remove the
1417
1443
// node from the tree.
1418
1444
if ( currentParentIsContainer ) {
@@ -1429,7 +1455,12 @@ function unmountHostComponents(
1429
1455
// Don't visit children because we already visited them.
1430
1456
} else if ( enableFundamentalAPI && node . tag === FundamentalComponent ) {
1431
1457
const fundamentalNode = node . stateNode . instance ;
1432
- commitNestedUnmounts ( finishedRoot , node , renderPriorityLevel ) ;
1458
+ commitNestedUnmounts (
1459
+ finishedRoot ,
1460
+ node ,
1461
+ nearestMountedAncestor ,
1462
+ renderPriorityLevel ,
1463
+ ) ;
1433
1464
// After all the children have unmounted, it is now safe to remove the
1434
1465
// node from the tree.
1435
1466
if ( currentParentIsContainer ) {
@@ -1481,7 +1512,12 @@ function unmountHostComponents(
1481
1512
continue ;
1482
1513
}
1483
1514
} else {
1484
- commitUnmount ( finishedRoot , node , renderPriorityLevel ) ;
1515
+ commitUnmount (
1516
+ finishedRoot ,
1517
+ node ,
1518
+ nearestMountedAncestor ,
1519
+ renderPriorityLevel ,
1520
+ ) ;
1485
1521
// Visit children because we may find more host components below.
1486
1522
if ( node . child !== null ) {
1487
1523
node . child . return = node ;
@@ -1511,15 +1547,26 @@ function unmountHostComponents(
1511
1547
function commitDeletion (
1512
1548
finishedRoot : FiberRoot ,
1513
1549
current : Fiber ,
1550
+ nearestMountedAncestor : Fiber ,
1514
1551
renderPriorityLevel : ReactPriorityLevel ,
1515
1552
) : void {
1516
1553
if ( supportsMutation ) {
1517
1554
// Recursively delete all host nodes from the parent.
1518
1555
// Detach refs and call componentWillUnmount() on the whole subtree.
1519
- unmountHostComponents ( finishedRoot , current , renderPriorityLevel ) ;
1556
+ unmountHostComponents (
1557
+ finishedRoot ,
1558
+ current ,
1559
+ nearestMountedAncestor ,
1560
+ renderPriorityLevel ,
1561
+ ) ;
1520
1562
} else {
1521
1563
// Detach refs and call componentWillUnmount() on the whole subtree.
1522
- commitNestedUnmounts ( finishedRoot , current , renderPriorityLevel ) ;
1564
+ commitNestedUnmounts (
1565
+ finishedRoot ,
1566
+ current ,
1567
+ nearestMountedAncestor ,
1568
+ renderPriorityLevel ,
1569
+ ) ;
1523
1570
}
1524
1571
const alternate = current . alternate ;
1525
1572
detachFiberMutation ( current ) ;
0 commit comments