Avoid conditions where control flow is sufficient (#24126)
This also fixes a type error since resetTextContent can only be called on Instances.
Sebastian Markbåge committed
Mar 18, 2022 at 16:51 UTC
dbe9e732af1f12757a55adb12a8279d7db898b60
2 files changed
+38
-54
packages/react-reconciler/src/ReactFiberCommitWork.new.js
+19
-27
@@ -1567,22 +1567,29 @@ function commitPlacement(finishedWork: Fiber): void {
1567
const parentFiber = getHostParentFiber(finishedWork);
1568
1569
// Note: these two variables *must* always be updated together.
1570
- let parent;
1571
- let isContainer;
1572
- const parentStateNode = parentFiber.stateNode;
1570
switch (parentFiber.tag) {
1574
- case HostComponent:
1575
- parent = parentStateNode;
1576
- isContainer = false;
1571
+ case HostComponent: {
1572
+ const parent: Instance = parentFiber.stateNode;
1573
+ if (parentFiber.flags & ContentReset) {
1574
+ // Reset the text content of the parent before doing any insertions
1575
+ resetTextContent(parent);
1576
+ // Clear ContentReset from the effect tag
1577
+ parentFiber.flags &= ~ContentReset;
1578
+ }
1579
+
1580
+ const before = getHostSibling(finishedWork);
1581
+ // We only have the top Fiber that was inserted but we need to recurse down its
1582
+ // children to find all the terminal nodes.
1583
+ insertOrAppendPlacementNode(finishedWork, before, parent);
1584
break;
1585
+ }
1586
case HostRoot:
1579
- parent = parentStateNode.containerInfo;
1580
- isContainer = true;
1581
- break;
1582
- case HostPortal:
1583
- parent = parentStateNode.containerInfo;
1584
- isContainer = true;
1587
+ case HostPortal: {
1588
+ const parent: Container = parentFiber.stateNode.containerInfo;
1589
+ const before = getHostSibling(finishedWork);
1590
+ insertOrAppendPlacementNodeIntoContainer(finishedWork, before, parent);
1591
break;
1592
+ }
1593
// eslint-disable-next-line-no-fallthrough
1594
default:
1595
throw new Error(
@@ -1590,21 +1597,6 @@ function commitPlacement(finishedWork: Fiber): void {
1597
'in React. Please file an issue.',
1598
);
1599
}
1593
- if (parentFiber.flags & ContentReset) {
1594
- // Reset the text content of the parent before doing any insertions
1595
- resetTextContent(parent);
1596
- // Clear ContentReset from the effect tag
1597
- parentFiber.flags &= ~ContentReset;
1598
- }
1599
-
1600
- const before = getHostSibling(finishedWork);
1601
- // We only have the top Fiber that was inserted but we need to recurse down its
1602
- // children to find all the terminal nodes.
1603
- if (isContainer) {
1604
- insertOrAppendPlacementNodeIntoContainer(finishedWork, before, parent);
1605
- } else {
1606
- insertOrAppendPlacementNode(finishedWork, before, parent);
1607
- }
1600
}
1601
1602
function insertOrAppendPlacementNodeIntoContainer(
packages/react-reconciler/src/ReactFiberCommitWork.old.js
+19
-27
@@ -1567,22 +1567,29 @@ function commitPlacement(finishedWork: Fiber): void {
1567
const parentFiber = getHostParentFiber(finishedWork);
1568
1569
// Note: these two variables *must* always be updated together.
1570
- let parent;
1571
- let isContainer;
1572
- const parentStateNode = parentFiber.stateNode;
1570
switch (parentFiber.tag) {
1574
- case HostComponent:
1575
- parent = parentStateNode;
1576
- isContainer = false;
1571
+ case HostComponent: {
1572
+ const parent: Instance = parentFiber.stateNode;
1573
+ if (parentFiber.flags & ContentReset) {
1574
+ // Reset the text content of the parent before doing any insertions
1575
+ resetTextContent(parent);
1576
+ // Clear ContentReset from the effect tag
1577
+ parentFiber.flags &= ~ContentReset;
1578
+ }
1579
+
1580
+ const before = getHostSibling(finishedWork);
1581
+ // We only have the top Fiber that was inserted but we need to recurse down its
1582
+ // children to find all the terminal nodes.
1583
+ insertOrAppendPlacementNode(finishedWork, before, parent);
1584
break;
1585
+ }
1586
case HostRoot:
1579
- parent = parentStateNode.containerInfo;
1580
- isContainer = true;
1581
- break;
1582
- case HostPortal:
1583
- parent = parentStateNode.containerInfo;
1584
- isContainer = true;
1587
+ case HostPortal: {
1588
+ const parent: Container = parentFiber.stateNode.containerInfo;
1589
+ const before = getHostSibling(finishedWork);
1590
+ insertOrAppendPlacementNodeIntoContainer(finishedWork, before, parent);
1591
break;
1592
+ }
1593
// eslint-disable-next-line-no-fallthrough
1594
default:
1595
throw new Error(
@@ -1590,21 +1597,6 @@ function commitPlacement(finishedWork: Fiber): void {
1597
'in React. Please file an issue.',
1598
);
1599
}
1593
- if (parentFiber.flags & ContentReset) {
1594
- // Reset the text content of the parent before doing any insertions
1595
- resetTextContent(parent);
1596
- // Clear ContentReset from the effect tag
1597
- parentFiber.flags &= ~ContentReset;
1598
- }
1599
-
1600
- const before = getHostSibling(finishedWork);
1601
- // We only have the top Fiber that was inserted but we need to recurse down its
1602
- // children to find all the terminal nodes.
1603
- if (isContainer) {
1604
- insertOrAppendPlacementNodeIntoContainer(finishedWork, before, parent);
1605
- } else {
1606
- insertOrAppendPlacementNode(finishedWork, before, parent);
1607
- }
1600
}
1601
1602
function insertOrAppendPlacementNodeIntoContainer(