Skip to content

Commit

Permalink
Move ref commit effects inside switch statement
Browse files Browse the repository at this point in the history
Only certain fiber types can have refs attached to them, so this moves the
Ref effect logic out of the common path and into the corresponding branch
of the layout phase's switch statement.

The types of fibers this affects are host components and class components.
Function components are not affected because they can only have a ref via
useImperativeHandle, which has a different implementation. The experimental
Scope type attaches its refs in the mutation phase, not the layout phase.
  • Loading branch information
acdlite committed Jul 12, 2022
1 parent e225fa4 commit b8c96b1
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 28 deletions.
25 changes: 11 additions & 14 deletions packages/react-reconciler/src/ReactFiberCommitWork.new.js
Original file line number Diff line number Diff line change
Expand Up @@ -893,6 +893,12 @@ function commitLayoutEffectOnFiber(
// TODO: revisit this when we implement resuming.
commitCallbacks(updateQueue, instance);
}

if (finishedWork.flags & Ref) {
if (!offscreenSubtreeWasHidden) {
commitAttachRef(finishedWork);
}
}
break;
}
case HostRoot: {
Expand Down Expand Up @@ -930,6 +936,11 @@ function commitLayoutEffectOnFiber(
commitMount(instance, type, props, finishedWork);
}

if (finishedWork.flags & Ref) {
if (!offscreenSubtreeWasHidden) {
commitAttachRef(finishedWork);
}
}
break;
}
case HostText: {
Expand Down Expand Up @@ -1020,20 +1031,6 @@ function commitLayoutEffectOnFiber(
);
}
}

if (!offscreenSubtreeWasHidden) {
if (enableScopeAPI) {
// TODO: This is a temporary solution that allowed us to transition away
// from React Flare on www.
if (finishedWork.flags & Ref && finishedWork.tag !== ScopeComponent) {
commitAttachRef(finishedWork);
}
} else {
if (finishedWork.flags & Ref) {
commitAttachRef(finishedWork);
}
}
}
}

function reappearLayoutEffectsOnFiber(node: Fiber) {
Expand Down
25 changes: 11 additions & 14 deletions packages/react-reconciler/src/ReactFiberCommitWork.old.js
Original file line number Diff line number Diff line change
Expand Up @@ -893,6 +893,12 @@ function commitLayoutEffectOnFiber(
// TODO: revisit this when we implement resuming.
commitCallbacks(updateQueue, instance);
}

if (finishedWork.flags & Ref) {
if (!offscreenSubtreeWasHidden) {
commitAttachRef(finishedWork);
}
}
break;
}
case HostRoot: {
Expand Down Expand Up @@ -930,6 +936,11 @@ function commitLayoutEffectOnFiber(
commitMount(instance, type, props, finishedWork);
}

if (finishedWork.flags & Ref) {
if (!offscreenSubtreeWasHidden) {
commitAttachRef(finishedWork);
}
}
break;
}
case HostText: {
Expand Down Expand Up @@ -1020,20 +1031,6 @@ function commitLayoutEffectOnFiber(
);
}
}

if (!offscreenSubtreeWasHidden) {
if (enableScopeAPI) {
// TODO: This is a temporary solution that allowed us to transition away
// from React Flare on www.
if (finishedWork.flags & Ref && finishedWork.tag !== ScopeComponent) {
commitAttachRef(finishedWork);
}
} else {
if (finishedWork.flags & Ref) {
commitAttachRef(finishedWork);
}
}
}
}

function reappearLayoutEffectsOnFiber(node: Fiber) {
Expand Down

0 comments on commit b8c96b1

Please sign in to comment.