mirror of
https://github.com/facebook/react.git
synced 2025-11-01 09:12:30 +00:00
DCE hooks code when flag is off (#14111)
This commit is contained in:
+7
-1
@@ -25,6 +25,7 @@ import describeComponentFrame from 'shared/describeComponentFrame';
|
||||
import ReactSharedInternals from 'shared/ReactSharedInternals';
|
||||
import {
|
||||
warnAboutDeprecatedLifecycles,
|
||||
enableHooks,
|
||||
enableSuspenseServerRenderer,
|
||||
} from 'shared/ReactFeatureFlags';
|
||||
|
||||
@@ -52,6 +53,7 @@ import {
|
||||
prepareToUseHooks,
|
||||
finishHooks,
|
||||
Dispatcher,
|
||||
DispatcherWithoutHooks,
|
||||
} from './ReactPartialRendererHooks';
|
||||
import {
|
||||
Namespaces,
|
||||
@@ -819,7 +821,11 @@ class ReactDOMServerRenderer {
|
||||
return null;
|
||||
}
|
||||
|
||||
ReactCurrentOwner.currentDispatcher = Dispatcher;
|
||||
if (enableHooks) {
|
||||
ReactCurrentOwner.currentDispatcher = Dispatcher;
|
||||
} else {
|
||||
ReactCurrentOwner.currentDispatcher = DispatcherWithoutHooks;
|
||||
}
|
||||
try {
|
||||
let out = '';
|
||||
while (out.length < bytes) {
|
||||
|
||||
@@ -350,3 +350,6 @@ export const Dispatcher = {
|
||||
// Effects are not run in the server environment.
|
||||
useEffect: noop,
|
||||
};
|
||||
export const DispatcherWithoutHooks = {
|
||||
readContext,
|
||||
};
|
||||
|
||||
@@ -22,6 +22,7 @@ import type {SuspenseState} from './ReactFiberSuspenseComponent';
|
||||
import type {FunctionComponentUpdateQueue} from './ReactFiberHooks';
|
||||
|
||||
import {
|
||||
enableHooks,
|
||||
enableSchedulerTracing,
|
||||
enableProfilerTimer,
|
||||
} from 'shared/ReactFeatureFlags';
|
||||
@@ -278,6 +279,9 @@ function commitHookEffectList(
|
||||
mountTag: number,
|
||||
finishedWork: Fiber,
|
||||
) {
|
||||
if (!enableHooks) {
|
||||
return;
|
||||
}
|
||||
const updateQueue: FunctionComponentUpdateQueue | null = (finishedWork.updateQueue: any);
|
||||
let lastEffect = updateQueue !== null ? updateQueue.lastEffect : null;
|
||||
if (lastEffect !== null) {
|
||||
|
||||
@@ -34,3 +34,6 @@ export const Dispatcher = {
|
||||
useRef,
|
||||
useState,
|
||||
};
|
||||
export const DispatcherWithoutHooks = {
|
||||
readContext,
|
||||
};
|
||||
|
||||
@@ -13,6 +13,7 @@ import type {ExpirationTime} from './ReactFiberExpirationTime';
|
||||
import type {HookEffectTag} from './ReactHookEffectTags';
|
||||
|
||||
import {NoWork} from './ReactFiberExpirationTime';
|
||||
import {enableHooks} from 'shared/ReactFeatureFlags';
|
||||
import {readContext} from './ReactFiberNewContext';
|
||||
import {
|
||||
Snapshot as SnapshotEffect,
|
||||
@@ -124,6 +125,9 @@ export function prepareToUseHooks(
|
||||
workInProgress: Fiber,
|
||||
nextRenderExpirationTime: ExpirationTime,
|
||||
): void {
|
||||
if (!enableHooks) {
|
||||
return;
|
||||
}
|
||||
renderExpirationTime = nextRenderExpirationTime;
|
||||
currentlyRenderingFiber = workInProgress;
|
||||
firstCurrentHook = current !== null ? current.memoizedState : null;
|
||||
@@ -147,6 +151,10 @@ export function finishHooks(
|
||||
children: any,
|
||||
refOrContext: any,
|
||||
): any {
|
||||
if (!enableHooks) {
|
||||
return children;
|
||||
}
|
||||
|
||||
// This must be called after every function component to prevent hooks from
|
||||
// being used in classes.
|
||||
|
||||
@@ -206,6 +214,10 @@ export function finishHooks(
|
||||
}
|
||||
|
||||
export function resetHooks(): void {
|
||||
if (!enableHooks) {
|
||||
return;
|
||||
}
|
||||
|
||||
// This is called instead of `finishHooks` if the component throws. It's also
|
||||
// called inside mountIndeterminateComponent if we determine the component
|
||||
// is a module-style component.
|
||||
|
||||
+13
-4
@@ -56,6 +56,7 @@ import {
|
||||
SimpleMemoComponent,
|
||||
} from 'shared/ReactWorkTags';
|
||||
import {
|
||||
enableHooks,
|
||||
enableSchedulerTracing,
|
||||
enableProfilerTimer,
|
||||
enableUserTimingAPI,
|
||||
@@ -164,7 +165,7 @@ import {
|
||||
commitDetachRef,
|
||||
commitPassiveHookEffects,
|
||||
} from './ReactFiberCommitWork';
|
||||
import {Dispatcher} from './ReactFiberDispatcher';
|
||||
import {Dispatcher, DispatcherWithoutHooks} from './ReactFiberDispatcher';
|
||||
|
||||
export type Thenable = {
|
||||
then(resolve: () => mixed, reject?: () => mixed): mixed,
|
||||
@@ -504,7 +505,7 @@ function commitAllLifeCycles(
|
||||
commitAttachRef(nextEffect);
|
||||
}
|
||||
|
||||
if (effectTag & Passive) {
|
||||
if (enableHooks && effectTag & Passive) {
|
||||
rootWithPendingPassiveEffects = finishedRoot;
|
||||
}
|
||||
|
||||
@@ -768,7 +769,11 @@ function commitRoot(root: FiberRoot, finishedWork: Fiber): void {
|
||||
}
|
||||
}
|
||||
|
||||
if (firstEffect !== null && rootWithPendingPassiveEffects !== null) {
|
||||
if (
|
||||
enableHooks &&
|
||||
firstEffect !== null &&
|
||||
rootWithPendingPassiveEffects !== null
|
||||
) {
|
||||
// This commit included a passive effect. These do not need to fire until
|
||||
// after the next paint. Schedule an callback to fire them in an async
|
||||
// event. To ensure serial execution, the callback will be flushed early if
|
||||
@@ -1192,7 +1197,11 @@ function renderRoot(root: FiberRoot, isYieldy: boolean): void {
|
||||
flushPassiveEffects();
|
||||
|
||||
isWorking = true;
|
||||
ReactCurrentOwner.currentDispatcher = Dispatcher;
|
||||
if (enableHooks) {
|
||||
ReactCurrentOwner.currentDispatcher = Dispatcher;
|
||||
} else {
|
||||
ReactCurrentOwner.currentDispatcher = DispatcherWithoutHooks;
|
||||
}
|
||||
|
||||
const expirationTime = root.nextExpirationTimeToWorkOn;
|
||||
|
||||
|
||||
Reference in New Issue
Block a user