Skip to content

Commit c4c87e0

Browse files
authored
Small cleanup in ReactFiberCompleteWork (#27681)
These are all functionally equivalent changes. - remove double negation and more explicit naming of `hadNoMutationsEffects` - use docblock syntax that's consumed by Flow - remove useless cast
1 parent 0e352ea commit c4c87e0

File tree

1 file changed

+24
-23
lines changed

1 file changed

+24
-23
lines changed

packages/react-reconciler/src/ReactFiberCompleteWork.js

Lines changed: 24 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -178,39 +178,44 @@ import {
178178
} from './ReactFiberTracingMarkerComponent';
179179
import {suspendCommit} from './ReactFiberThenable';
180180

181+
/**
182+
* Tag the fiber with an update effect. This turns a Placement into
183+
* a PlacementAndUpdate.
184+
*/
181185
function markUpdate(workInProgress: Fiber) {
182-
// Tag the fiber with an update effect. This turns a Placement into
183-
// a PlacementAndUpdate.
184186
workInProgress.flags |= Update;
185187
}
186188

187189
function markRef(workInProgress: Fiber) {
188190
workInProgress.flags |= Ref | RefStatic;
189191
}
190192

191-
function hadNoMutationsEffects(current: null | Fiber, completedWork: Fiber) {
193+
/**
194+
* In persistent mode, return whether this update needs to clone the subtree.
195+
*/
196+
function doesRequireClone(current: null | Fiber, completedWork: Fiber) {
192197
const didBailout = current !== null && current.child === completedWork.child;
193198
if (didBailout) {
194-
return true;
199+
return false;
195200
}
196201

197202
if ((completedWork.flags & ChildDeletion) !== NoFlags) {
198-
return false;
203+
return true;
199204
}
200205

201-
// TODO: If we move the `hadNoMutationsEffects` call after `bubbleProperties`
206+
// TODO: If we move the `doesRequireClone` call after `bubbleProperties`
202207
// then we only have to check the `completedWork.subtreeFlags`.
203208
let child = completedWork.child;
204209
while (child !== null) {
205210
if (
206211
(child.flags & MutationMask) !== NoFlags ||
207212
(child.subtreeFlags & MutationMask) !== NoFlags
208213
) {
209-
return false;
214+
return true;
210215
}
211216
child = child.sibling;
212217
}
213-
return true;
218+
return false;
214219
}
215220

216221
function appendAllChildren(
@@ -303,7 +308,6 @@ function appendAllChildren(
303308
node = node.child;
304309
continue;
305310
}
306-
node = (node: Fiber);
307311
if (node === workInProgress) {
308312
return;
309313
}
@@ -400,15 +404,12 @@ function appendAllChildrenToContainer(
400404

401405
function updateHostContainer(current: null | Fiber, workInProgress: Fiber) {
402406
if (supportsPersistence) {
403-
const portalOrRoot: {
404-
containerInfo: Container,
405-
pendingChildren: ChildSet,
406-
...
407-
} = workInProgress.stateNode;
408-
const childrenUnchanged = hadNoMutationsEffects(current, workInProgress);
409-
if (childrenUnchanged) {
410-
// No changes, just reuse the existing instance.
411-
} else {
407+
if (doesRequireClone(current, workInProgress)) {
408+
const portalOrRoot: {
409+
containerInfo: Container,
410+
pendingChildren: ChildSet,
411+
...
412+
} = workInProgress.stateNode;
412413
const container = portalOrRoot.containerInfo;
413414
const newChildSet = createContainerChildSet();
414415
// If children might have changed, we have to add them all to the set.
@@ -449,8 +450,8 @@ function updateHostComponent(
449450
const oldProps = current.memoizedProps;
450451
// If there are no effects associated with this node, then none of our children had any updates.
451452
// This guarantees that we can reuse all of them.
452-
const childrenUnchanged = hadNoMutationsEffects(current, workInProgress);
453-
if (childrenUnchanged && oldProps === newProps) {
453+
const requiresClone = doesRequireClone(current, workInProgress);
454+
if (!requiresClone && oldProps === newProps) {
454455
// No changes, just reuse the existing instance.
455456
// Note that this might release a previous clone.
456457
workInProgress.stateNode = currentInstance;
@@ -459,7 +460,7 @@ function updateHostComponent(
459460
const currentHostContext = getHostContext();
460461

461462
let newChildSet = null;
462-
if (!childrenUnchanged && passChildrenWhenCloningPersistedNodes) {
463+
if (requiresClone && passChildrenWhenCloningPersistedNodes) {
463464
newChildSet = createContainerChildSet();
464465
// If children might have changed, we have to add them all to the set.
465466
appendAllChildrenToContainer(
@@ -475,7 +476,7 @@ function updateHostComponent(
475476
type,
476477
oldProps,
477478
newProps,
478-
childrenUnchanged,
479+
!requiresClone,
479480
newChildSet,
480481
);
481482
if (newInstance === currentInstance) {
@@ -494,7 +495,7 @@ function updateHostComponent(
494495
markUpdate(workInProgress);
495496
}
496497
workInProgress.stateNode = newInstance;
497-
if (childrenUnchanged) {
498+
if (!requiresClone) {
498499
// If there are no other effects in this tree, we need to flag this node as having one.
499500
// Even though we're not going to use it for anything.
500501
// Otherwise parents won't know that there are new children to propagate upwards.

0 commit comments

Comments
 (0)