Skip to content

Commit b8c96b1

Browse files
committed
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.
1 parent e225fa4 commit b8c96b1

File tree

2 files changed

+22
-28
lines changed

2 files changed

+22
-28
lines changed

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

Lines changed: 11 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -893,6 +893,12 @@ function commitLayoutEffectOnFiber(
893893
// TODO: revisit this when we implement resuming.
894894
commitCallbacks(updateQueue, instance);
895895
}
896+
897+
if (finishedWork.flags & Ref) {
898+
if (!offscreenSubtreeWasHidden) {
899+
commitAttachRef(finishedWork);
900+
}
901+
}
896902
break;
897903
}
898904
case HostRoot: {
@@ -930,6 +936,11 @@ function commitLayoutEffectOnFiber(
930936
commitMount(instance, type, props, finishedWork);
931937
}
932938

939+
if (finishedWork.flags & Ref) {
940+
if (!offscreenSubtreeWasHidden) {
941+
commitAttachRef(finishedWork);
942+
}
943+
}
933944
break;
934945
}
935946
case HostText: {
@@ -1020,20 +1031,6 @@ function commitLayoutEffectOnFiber(
10201031
);
10211032
}
10221033
}
1023-
1024-
if (!offscreenSubtreeWasHidden) {
1025-
if (enableScopeAPI) {
1026-
// TODO: This is a temporary solution that allowed us to transition away
1027-
// from React Flare on www.
1028-
if (finishedWork.flags & Ref && finishedWork.tag !== ScopeComponent) {
1029-
commitAttachRef(finishedWork);
1030-
}
1031-
} else {
1032-
if (finishedWork.flags & Ref) {
1033-
commitAttachRef(finishedWork);
1034-
}
1035-
}
1036-
}
10371034
}
10381035

10391036
function reappearLayoutEffectsOnFiber(node: Fiber) {

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

Lines changed: 11 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -893,6 +893,12 @@ function commitLayoutEffectOnFiber(
893893
// TODO: revisit this when we implement resuming.
894894
commitCallbacks(updateQueue, instance);
895895
}
896+
897+
if (finishedWork.flags & Ref) {
898+
if (!offscreenSubtreeWasHidden) {
899+
commitAttachRef(finishedWork);
900+
}
901+
}
896902
break;
897903
}
898904
case HostRoot: {
@@ -930,6 +936,11 @@ function commitLayoutEffectOnFiber(
930936
commitMount(instance, type, props, finishedWork);
931937
}
932938

939+
if (finishedWork.flags & Ref) {
940+
if (!offscreenSubtreeWasHidden) {
941+
commitAttachRef(finishedWork);
942+
}
943+
}
933944
break;
934945
}
935946
case HostText: {
@@ -1020,20 +1031,6 @@ function commitLayoutEffectOnFiber(
10201031
);
10211032
}
10221033
}
1023-
1024-
if (!offscreenSubtreeWasHidden) {
1025-
if (enableScopeAPI) {
1026-
// TODO: This is a temporary solution that allowed us to transition away
1027-
// from React Flare on www.
1028-
if (finishedWork.flags & Ref && finishedWork.tag !== ScopeComponent) {
1029-
commitAttachRef(finishedWork);
1030-
}
1031-
} else {
1032-
if (finishedWork.flags & Ref) {
1033-
commitAttachRef(finishedWork);
1034-
}
1035-
}
1036-
}
10371034
}
10381035

10391036
function reappearLayoutEffectsOnFiber(node: Fiber) {

0 commit comments

Comments
 (0)