Move ref commit effects inside switch statement

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.
This commit is contained in:
Andrew Clark 2022-07-08 13:10:14 -04:00
parent e225fa43ad
commit b8c96b136d
2 changed files with 22 additions and 28 deletions

View File

@ -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: {
@ -930,6 +936,11 @@ function commitLayoutEffectOnFiber(
commitMount(instance, type, props, finishedWork);
}
if (finishedWork.flags & Ref) {
if (!offscreenSubtreeWasHidden) {
commitAttachRef(finishedWork);
}
}
break;
}
case HostText: {
@ -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) {

View File

@ -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: {
@ -930,6 +936,11 @@ function commitLayoutEffectOnFiber(
commitMount(instance, type, props, finishedWork);
}
if (finishedWork.flags & Ref) {
if (!offscreenSubtreeWasHidden) {
commitAttachRef(finishedWork);
}
}
break;
}
case HostText: {
@ -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) {