Skip to content

Commit 115bba0

Browse files
committed
DCE hooks code when flag is off
1 parent 8eca0ef commit 115bba0

File tree

6 files changed

+42
-5
lines changed

6 files changed

+42
-5
lines changed

packages/react-dom/src/server/ReactPartialRenderer.js

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@ import describeComponentFrame from 'shared/describeComponentFrame';
2525
import ReactSharedInternals from 'shared/ReactSharedInternals';
2626
import {
2727
warnAboutDeprecatedLifecycles,
28+
enableHooks,
2829
enableSuspenseServerRenderer,
2930
} from 'shared/ReactFeatureFlags';
3031

@@ -52,6 +53,7 @@ import {
5253
prepareToUseHooks,
5354
finishHooks,
5455
Dispatcher,
56+
DispatcherWithoutHooks,
5557
} from './ReactPartialRendererHooks';
5658
import {
5759
Namespaces,
@@ -819,7 +821,11 @@ class ReactDOMServerRenderer {
819821
return null;
820822
}
821823

822-
ReactCurrentOwner.currentDispatcher = Dispatcher;
824+
if (enableHooks) {
825+
ReactCurrentOwner.currentDispatcher = Dispatcher;
826+
} else {
827+
ReactCurrentOwner.currentDispatcher = DispatcherWithoutHooks;
828+
}
823829
try {
824830
let out = '';
825831
while (out.length < bytes) {

packages/react-dom/src/server/ReactPartialRendererHooks.js

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -350,3 +350,6 @@ export const Dispatcher = {
350350
// Effects are not run in the server environment.
351351
useEffect: noop,
352352
};
353+
export const DispatcherWithoutHooks = {
354+
readContext,
355+
};

packages/react-reconciler/src/ReactFiberCommitWork.js

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@ import type {SuspenseState} from './ReactFiberSuspenseComponent';
2222
import type {FunctionComponentUpdateQueue} from './ReactFiberHooks';
2323

2424
import {
25+
enableHooks,
2526
enableSchedulerTracing,
2627
enableProfilerTimer,
2728
} from 'shared/ReactFeatureFlags';
@@ -278,6 +279,9 @@ function commitHookEffectList(
278279
mountTag: number,
279280
finishedWork: Fiber,
280281
) {
282+
if (!enableHooks) {
283+
return;
284+
}
281285
const updateQueue: FunctionComponentUpdateQueue | null = (finishedWork.updateQueue: any);
282286
let lastEffect = updateQueue !== null ? updateQueue.lastEffect : null;
283287
if (lastEffect !== null) {

packages/react-reconciler/src/ReactFiberDispatcher.js

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,3 +34,6 @@ export const Dispatcher = {
3434
useRef,
3535
useState,
3636
};
37+
export const DispatcherWithoutHooks = {
38+
readContext,
39+
};

packages/react-reconciler/src/ReactFiberHooks.js

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ import type {ExpirationTime} from './ReactFiberExpirationTime';
1313
import type {HookEffectTag} from './ReactHookEffectTags';
1414

1515
import {NoWork} from './ReactFiberExpirationTime';
16+
import {enableHooks} from 'shared/ReactFeatureFlags';
1617
import {readContext} from './ReactFiberNewContext';
1718
import {
1819
Snapshot as SnapshotEffect,
@@ -124,6 +125,9 @@ export function prepareToUseHooks(
124125
workInProgress: Fiber,
125126
nextRenderExpirationTime: ExpirationTime,
126127
): void {
128+
if (!enableHooks) {
129+
return;
130+
}
127131
renderExpirationTime = nextRenderExpirationTime;
128132
currentlyRenderingFiber = workInProgress;
129133
firstCurrentHook = current !== null ? current.memoizedState : null;
@@ -147,6 +151,10 @@ export function finishHooks(
147151
children: any,
148152
refOrContext: any,
149153
): any {
154+
if (!enableHooks) {
155+
return;
156+
}
157+
150158
// This must be called after every function component to prevent hooks from
151159
// being used in classes.
152160

@@ -206,6 +214,10 @@ export function finishHooks(
206214
}
207215

208216
export function resetHooks(): void {
217+
if (!enableHooks) {
218+
return;
219+
}
220+
209221
// This is called instead of `finishHooks` if the component throws. It's also
210222
// called inside mountIndeterminateComponent if we determine the component
211223
// is a module-style component.

packages/react-reconciler/src/ReactFiberScheduler.js

Lines changed: 13 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -56,6 +56,7 @@ import {
5656
SimpleMemoComponent,
5757
} from 'shared/ReactWorkTags';
5858
import {
59+
enableHooks,
5960
enableSchedulerTracing,
6061
enableProfilerTimer,
6162
enableUserTimingAPI,
@@ -164,7 +165,7 @@ import {
164165
commitDetachRef,
165166
commitPassiveHookEffects,
166167
} from './ReactFiberCommitWork';
167-
import {Dispatcher} from './ReactFiberDispatcher';
168+
import {Dispatcher, DispatcherWithoutHooks} from './ReactFiberDispatcher';
168169

169170
export type Thenable = {
170171
then(resolve: () => mixed, reject?: () => mixed): mixed,
@@ -504,7 +505,7 @@ function commitAllLifeCycles(
504505
commitAttachRef(nextEffect);
505506
}
506507

507-
if (effectTag & Passive) {
508+
if (enableHooks && effectTag & Passive) {
508509
rootWithPendingPassiveEffects = finishedRoot;
509510
}
510511

@@ -768,7 +769,11 @@ function commitRoot(root: FiberRoot, finishedWork: Fiber): void {
768769
}
769770
}
770771

771-
if (firstEffect !== null && rootWithPendingPassiveEffects !== null) {
772+
if (
773+
enableHooks &&
774+
firstEffect !== null &&
775+
rootWithPendingPassiveEffects !== null
776+
) {
772777
// This commit included a passive effect. These do not need to fire until
773778
// after the next paint. Schedule an callback to fire them in an async
774779
// event. To ensure serial execution, the callback will be flushed early if
@@ -1192,7 +1197,11 @@ function renderRoot(root: FiberRoot, isYieldy: boolean): void {
11921197
flushPassiveEffects();
11931198

11941199
isWorking = true;
1195-
ReactCurrentOwner.currentDispatcher = Dispatcher;
1200+
if (enableHooks) {
1201+
ReactCurrentOwner.currentDispatcher = Dispatcher;
1202+
} else {
1203+
ReactCurrentOwner.currentDispatcher = DispatcherWithoutHooks;
1204+
}
11961205

11971206
const expirationTime = root.nextExpirationTimeToWorkOn;
11981207

0 commit comments

Comments
 (0)