Track currentOwner for Fiber locally inside the reconciler

This means that usage internal to the reconciler can refer to it directly
without going through the dispatcher.
This commit is contained in:
Sebastian Markbage
2024-04-22 12:05:45 -04:00
committed by Rick Hanlon
parent 84d0305f89
commit b217f5adf1
6 changed files with 27 additions and 19 deletions
+2 -2
View File
@@ -61,7 +61,7 @@ import {LegacyRoot} from 'react-reconciler/src/ReactRootTags';
import getComponentNameFromType from 'shared/getComponentNameFromType';
import {has as hasInstance} from 'shared/ReactInstanceMap';
import ReactSharedInternals from 'shared/ReactSharedInternals';
import {currentOwner} from 'react-reconciler/src/ReactFiberAsyncDispatcher';
import assign from 'shared/assign';
@@ -342,7 +342,7 @@ export function findDOMNode(
componentOrElement: Element | ?React$Component<any, any>,
): null | Element | Text {
if (__DEV__) {
const owner = (ReactSharedInternals.owner: any);
const owner = currentOwner;
if (owner !== null && owner.stateNode !== null) {
const warnedAboutRefsInRender = owner.stateNode._warnedAboutRefsInRender;
if (!warnedAboutRefsInRender) {
@@ -24,14 +24,14 @@ import {
findHostInstanceWithWarning,
} from 'react-reconciler/src/ReactFiberReconciler';
import {doesFiberContain} from 'react-reconciler/src/ReactFiberTreeReflection';
import ReactSharedInternals from 'shared/ReactSharedInternals';
import getComponentNameFromType from 'shared/getComponentNameFromType';
import {currentOwner} from 'react-reconciler/src/ReactFiberAsyncDispatcher';
export function findHostInstance_DEPRECATED<TElementType: ElementType>(
componentOrHandle: ?(ElementRef<TElementType> | number),
): ?ElementRef<HostComponent<mixed>> {
if (__DEV__) {
const owner = ReactSharedInternals.owner;
const owner = currentOwner;
if (owner !== null && owner.stateNode !== null) {
if (!owner.stateNode._warnedAboutRefsInRender) {
console.error(
@@ -86,7 +86,7 @@ export function findHostInstance_DEPRECATED<TElementType: ElementType>(
export function findNodeHandle(componentOrHandle: any): ?number {
if (__DEV__) {
const owner = ReactSharedInternals.owner;
const owner = currentOwner;
if (owner !== null && owner.stateNode !== null) {
if (!owner.stateNode._warnedAboutRefsInRender) {
console.error(
+7 -2
View File
@@ -29,13 +29,18 @@ function getCacheForType<T>(resourceType: () => T): T {
return cacheForType;
}
export let currentOwner: Fiber | null = null;
export const DefaultAsyncDispatcher: AsyncDispatcher = ({
getCacheForType,
}: any);
if (__DEV__ || !disableStringRefs) {
DefaultAsyncDispatcher.getOwner = (): null | Fiber => {
// TODO
return null;
return currentOwner;
};
}
export function setCurrentOwner(fiber: null | Fiber) {
currentOwner = fiber;
}
+5 -5
View File
@@ -91,7 +91,6 @@ import {
Passive,
DidDefer,
} from './ReactFiberFlags';
import ReactSharedInternals from 'shared/ReactSharedInternals';
import {
debugRenderPhaseSideEffectsForStrictMode,
disableLegacyContext,
@@ -297,6 +296,7 @@ import {
pushRootMarkerInstance,
TransitionTracingMarker,
} from './ReactFiberTracingMarkerComponent';
import {setCurrentOwner} from './ReactFiberAsyncDispatcher';
// A special exception that's used to unwind the stack when an update flows
// into a dehydrated boundary.
@@ -432,7 +432,7 @@ function updateForwardRef(
markComponentRenderStarted(workInProgress);
}
if (__DEV__) {
ReactSharedInternals.owner = workInProgress;
setCurrentOwner(workInProgress);
setIsRendering(true);
nextChildren = renderWithHooks(
current,
@@ -1150,7 +1150,7 @@ function updateFunctionComponent(
markComponentRenderStarted(workInProgress);
}
if (__DEV__) {
ReactSharedInternals.owner = workInProgress;
setCurrentOwner(workInProgress);
setIsRendering(true);
nextChildren = renderWithHooks(
current,
@@ -1373,7 +1373,7 @@ function finishClassComponent(
// Rerender
if (__DEV__ || !disableStringRefs) {
ReactSharedInternals.owner = workInProgress;
setCurrentOwner(workInProgress);
}
let nextChildren;
if (
@@ -3419,7 +3419,7 @@ function updateContextConsumer(
}
let newChildren;
if (__DEV__) {
ReactSharedInternals.owner = workInProgress;
setCurrentOwner(workInProgress);
setIsRendering(true);
newChildren = render(newValue);
setIsRendering(false);
+2 -2
View File
@@ -12,7 +12,6 @@ import type {Container, SuspenseInstance} from './ReactFiberConfig';
import type {SuspenseState} from './ReactFiberSuspenseComponent';
import {get as getInstance} from 'shared/ReactInstanceMap';
import ReactSharedInternals from 'shared/ReactSharedInternals';
import getComponentNameFromFiber from 'react-reconciler/src/getComponentNameFromFiber';
import {
ClassComponent,
@@ -25,6 +24,7 @@ import {
SuspenseComponent,
} from './ReactWorkTags';
import {NoFlags, Placement, Hydrating} from './ReactFiberFlags';
import {currentOwner} from './ReactFiberAsyncDispatcher';
export function getNearestMountedFiber(fiber: Fiber): null | Fiber {
let node = fiber;
@@ -89,7 +89,7 @@ export function isFiberMounted(fiber: Fiber): boolean {
export function isMounted(component: React$Component<any, any>): boolean {
if (__DEV__) {
const owner = (ReactSharedInternals.owner: any);
const owner = currentOwner;
if (owner !== null && owner.tag === ClassComponent) {
const ownerFiber: Fiber = owner;
const instance = ownerFiber.stateNode;
+8 -5
View File
@@ -203,7 +203,10 @@ import {
resetHooksOnUnwind,
ContextOnlyDispatcher,
} from './ReactFiberHooks';
import {DefaultAsyncDispatcher} from './ReactFiberAsyncDispatcher';
import {
DefaultAsyncDispatcher,
setCurrentOwner,
} from './ReactFiberAsyncDispatcher';
import {
createCapturedValueAtFiber,
type CapturedValue,
@@ -1684,7 +1687,7 @@ function handleThrow(root: FiberRoot, thrownValue: any): void {
resetHooksAfterThrow();
resetCurrentDebugFiberInDEV();
if (__DEV__ || !disableStringRefs) {
ReactSharedInternals.owner = null;
setCurrentOwner(null);
}
if (thrownValue === SuspenseException) {
@@ -2386,7 +2389,7 @@ function performUnitOfWork(unitOfWork: Fiber): void {
}
if (__DEV__ || !disableStringRefs) {
ReactSharedInternals.owner = null;
setCurrentOwner(null);
}
}
@@ -2501,7 +2504,7 @@ function replaySuspendedUnitOfWork(unitOfWork: Fiber): void {
}
if (__DEV__ || !disableStringRefs) {
ReactSharedInternals.owner = null;
setCurrentOwner(null);
}
}
@@ -2894,7 +2897,7 @@ function commitRootImpl(
// Reset this to null before calling lifecycles
if (__DEV__ || !disableStringRefs) {
ReactSharedInternals.owner = null;
setCurrentOwner(null);
}
// The commit phase is broken into several sub-phases. We do a separate pass