Revert "Refactor commitPlacement to recursively insert nodes (#17996)" (#18517)
This reverts commit df5faddcc2ad27be5700823d4f5367e5e9ae4620.
Dan Abramov committed
Apr 7, 2020 at 13:41 UTC
e69ca310ea56b6f2f510dd463902405285953432
1 file changed
+33
-61
packages/react-reconciler/src/ReactFiberCommitWork.js
+33
-61
@@ -1283,72 +1283,44 @@ function commitPlacement(finishedWork: Fiber): void {
1283
const before = getHostSibling(finishedWork);
1284
// We only have the top Fiber that was inserted but we need to recurse down its
1285
// children to find all the terminal nodes.
1286
- if (isContainer) {
1287
- insertOrAppendPlacementNodeIntoContainer(finishedWork, before, parent);
1288
- } else {
1289
- insertOrAppendPlacementNode(finishedWork, before, parent);
1290
- }
1291
-}
1292
-
1293
-function insertOrAppendPlacementNodeIntoContainer(
1294
- node: Fiber,
1295
- before: ?Instance,
1296
- parent: Container,
1297
-): void {
1298
- const {tag} = node;
1299
- const isHost = tag === HostComponent || tag === HostText;
1300
- if (isHost || (enableFundamentalAPI && tag === FundamentalComponent)) {
1301
- const stateNode = isHost ? node.stateNode : node.stateNode.instance;
1302
- if (before) {
1303
- insertInContainerBefore(parent, stateNode, before);
1304
- } else {
1305
- appendChildToContainer(parent, stateNode);
1306
- }
1307
- } else if (tag === HostPortal) {
1308
- // If the insertion itself is a portal, then we don't want to traverse
1309
- // down its children. Instead, we'll get insertions from each child in
1310
- // the portal directly.
1311
- } else {
1312
- const child = node.child;
1313
- if (child !== null) {
1314
- insertOrAppendPlacementNodeIntoContainer(child, before, parent);
1315
- let sibling = child.sibling;
1316
- while (sibling !== null) {
1317
- insertOrAppendPlacementNodeIntoContainer(sibling, before, parent);
1318
- sibling = sibling.sibling;
1286
+ let node: Fiber = finishedWork;
1287
+ while (true) {
1288
+ const isHost = node.tag === HostComponent || node.tag === HostText;
1289
+ if (isHost || (enableFundamentalAPI && node.tag === FundamentalComponent)) {
1290
+ const stateNode = isHost ? node.stateNode : node.stateNode.instance;
1291
+ if (before) {
1292
+ if (isContainer) {
1293
+ insertInContainerBefore(parent, stateNode, before);
1294
+ } else {
1295
+ insertBefore(parent, stateNode, before);
1296
+ }
1297
+ } else {
1298
+ if (isContainer) {
1299
+ appendChildToContainer(parent, stateNode);
1300
+ } else {
1301
+ appendChild(parent, stateNode);
1302
+ }
1303
}
1304
+ } else if (node.tag === HostPortal) {
1305
+ // If the insertion itself is a portal, then we don't want to traverse
1306
+ // down its children. Instead, we'll get insertions from each child in
1307
+ // the portal directly.
1308
+ } else if (node.child !== null) {
1309
+ node.child.return = node;
1310
+ node = node.child;
1311
+ continue;
1312
}
1321
- }
1322
-}
1323
-
1324
-function insertOrAppendPlacementNode(
1325
- node: Fiber,
1326
- before: ?Instance,
1327
- parent: Instance,
1328
-): void {
1329
- const {tag} = node;
1330
- const isHost = tag === HostComponent || tag === HostText;
1331
- if (isHost || (enableFundamentalAPI && tag === FundamentalComponent)) {
1332
- const stateNode = isHost ? node.stateNode : node.stateNode.instance;
1333
- if (before) {
1334
- insertBefore(parent, stateNode, before);
1335
- } else {
1336
- appendChild(parent, stateNode);
1313
+ if (node === finishedWork) {
1314
+ return;
1315
}
1338
- } else if (tag === HostPortal) {
1339
- // If the insertion itself is a portal, then we don't want to traverse
1340
- // down its children. Instead, we'll get insertions from each child in
1341
- // the portal directly.
1342
- } else {
1343
- const child = node.child;
1344
- if (child !== null) {
1345
- insertOrAppendPlacementNode(child, before, parent);
1346
- let sibling = child.sibling;
1347
- while (sibling !== null) {
1348
- insertOrAppendPlacementNode(sibling, before, parent);
1349
- sibling = sibling.sibling;
1316
+ while (node.sibling === null) {
1317
+ if (node.return === null || node.return === finishedWork) {
1318
+ return;
1319
}
1320
+ node = node.return;
1321
}
1322
+ node.sibling.return = node.return;
1323
+ node = node.sibling;
1324
}
1325
}
1326