Skip to content

Commit dbe9e73

Browse files
authored
Avoid conditions where control flow is sufficient (#24126)
This also fixes a type error since resetTextContent can only be called on Instances.
1 parent 645ec5d commit dbe9e73

File tree

2 files changed

+38
-54
lines changed

2 files changed

+38
-54
lines changed

packages/react-reconciler/src/ReactFiberCommitWork.new.js

+19-27
Original file line numberDiff line numberDiff line change
@@ -1567,44 +1567,36 @@ function commitPlacement(finishedWork: Fiber): void {
15671567
const parentFiber = getHostParentFiber(finishedWork);
15681568

15691569
// Note: these two variables *must* always be updated together.
1570-
let parent;
1571-
let isContainer;
1572-
const parentStateNode = parentFiber.stateNode;
15731570
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);
15771584
break;
1585+
}
15781586
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);
15851591
break;
1592+
}
15861593
// eslint-disable-next-line-no-fallthrough
15871594
default:
15881595
throw new Error(
15891596
'Invalid host parent fiber. This error is likely caused by a bug ' +
15901597
'in React. Please file an issue.',
15911598
);
15921599
}
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-
}
16081600
}
16091601

16101602
function insertOrAppendPlacementNodeIntoContainer(

packages/react-reconciler/src/ReactFiberCommitWork.old.js

+19-27
Original file line numberDiff line numberDiff line change
@@ -1567,44 +1567,36 @@ function commitPlacement(finishedWork: Fiber): void {
15671567
const parentFiber = getHostParentFiber(finishedWork);
15681568

15691569
// Note: these two variables *must* always be updated together.
1570-
let parent;
1571-
let isContainer;
1572-
const parentStateNode = parentFiber.stateNode;
15731570
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);
15771584
break;
1585+
}
15781586
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);
15851591
break;
1592+
}
15861593
// eslint-disable-next-line-no-fallthrough
15871594
default:
15881595
throw new Error(
15891596
'Invalid host parent fiber. This error is likely caused by a bug ' +
15901597
'in React. Please file an issue.',
15911598
);
15921599
}
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-
}
16081600
}
16091601

16101602
function insertOrAppendPlacementNodeIntoContainer(

0 commit comments

Comments
 (0)