@samitouri / QOS-React / commits / ebdb47d2c1

DCE hooks code when flag is off (#14111)

Sophie Alpert committed Nov 5, 2018 at 13:04 UTC ebdb47d2c137b399b7d9b4468885887961d9cec5
6 files changed +42 -5
packages/react-dom/src/server/ReactPartialRenderer.js
+7 -1
@@ -25,6 +25,7 @@ import describeComponentFrame from 'shared/describeComponentFrame';
25 import ReactSharedInternals from 'shared/ReactSharedInternals';
26 import {
27 warnAboutDeprecatedLifecycles,
28 + enableHooks,
29 enableSuspenseServerRenderer,
30 } from 'shared/ReactFeatureFlags';
31
@@ -52,6 +53,7 @@ import {
53 prepareToUseHooks,
54 finishHooks,
55 Dispatcher,
56 + DispatcherWithoutHooks,
57 } from './ReactPartialRendererHooks';
58 import {
59 Namespaces,
@@ -819,7 +821,11 @@ class ReactDOMServerRenderer {
821 return null;
822 }
823
822 - ReactCurrentOwner.currentDispatcher = Dispatcher;
824 + if (enableHooks) {
825 + ReactCurrentOwner.currentDispatcher = Dispatcher;
826 + } else {
827 + ReactCurrentOwner.currentDispatcher = DispatcherWithoutHooks;
828 + }
829 try {
830 let out = '';
831 while (out.length < bytes) {
packages/react-dom/src/server/ReactPartialRendererHooks.js
+3
@@ -350,3 +350,6 @@ export const Dispatcher = {
350 // Effects are not run in the server environment.
351 useEffect: noop,
352 };
353 +export const DispatcherWithoutHooks = {
354 + readContext,
355 +};
packages/react-reconciler/src/ReactFiberCommitWork.js
+4
@@ -22,6 +22,7 @@ import type {SuspenseState} from './ReactFiberSuspenseComponent';
22 import type {FunctionComponentUpdateQueue} from './ReactFiberHooks';
23
24 import {
25 + enableHooks,
26 enableSchedulerTracing,
27 enableProfilerTimer,
28 } from 'shared/ReactFeatureFlags';
@@ -278,6 +279,9 @@ function commitHookEffectList(
279 mountTag: number,
280 finishedWork: Fiber,
281 ) {
282 + if (!enableHooks) {
283 + return;
284 + }
285 const updateQueue: FunctionComponentUpdateQueue | null = (finishedWork.updateQueue: any);
286 let lastEffect = updateQueue !== null ? updateQueue.lastEffect : null;
287 if (lastEffect !== null) {
packages/react-reconciler/src/ReactFiberDispatcher.js
+3
@@ -34,3 +34,6 @@ export const Dispatcher = {
34 useRef,
35 useState,
36 };
37 +export const DispatcherWithoutHooks = {
38 + readContext,
39 +};
packages/react-reconciler/src/ReactFiberHooks.js
+12
@@ -13,6 +13,7 @@ import type {ExpirationTime} from './ReactFiberExpirationTime';
13 import type {HookEffectTag} from './ReactHookEffectTags';
14
15 import {NoWork} from './ReactFiberExpirationTime';
16 +import {enableHooks} from 'shared/ReactFeatureFlags';
17 import {readContext} from './ReactFiberNewContext';
18 import {
19 Snapshot as SnapshotEffect,
@@ -124,6 +125,9 @@ export function prepareToUseHooks(
125 workInProgress: Fiber,
126 nextRenderExpirationTime: ExpirationTime,
127 ): void {
128 + if (!enableHooks) {
129 + return;
130 + }
131 renderExpirationTime = nextRenderExpirationTime;
132 currentlyRenderingFiber = workInProgress;
133 firstCurrentHook = current !== null ? current.memoizedState : null;
@@ -147,6 +151,10 @@ export function finishHooks(
151 children: any,
152 refOrContext: any,
153 ): any {
154 + if (!enableHooks) {
155 + return children;
156 + }
157 +
158 // This must be called after every function component to prevent hooks from
159 // being used in classes.
160
@@ -206,6 +214,10 @@ export function finishHooks(
214 }
215
216 export function resetHooks(): void {
217 + if (!enableHooks) {
218 + return;
219 + }
220 +
221 // This is called instead of `finishHooks` if the component throws. It's also
222 // called inside mountIndeterminateComponent if we determine the component
223 // is a module-style component.
packages/react-reconciler/src/ReactFiberScheduler.js
+13 -4
@@ -56,6 +56,7 @@ import {
56 SimpleMemoComponent,
57 } from 'shared/ReactWorkTags';
58 import {
59 + enableHooks,
60 enableSchedulerTracing,
61 enableProfilerTimer,
62 enableUserTimingAPI,
@@ -164,7 +165,7 @@ import {
165 commitDetachRef,
166 commitPassiveHookEffects,
167 } from './ReactFiberCommitWork';
167 -import {Dispatcher} from './ReactFiberDispatcher';
168 +import {Dispatcher, DispatcherWithoutHooks} from './ReactFiberDispatcher';
169
170 export type Thenable = {
171 then(resolve: () => mixed, reject?: () => mixed): mixed,
@@ -504,7 +505,7 @@ function commitAllLifeCycles(
505 commitAttachRef(nextEffect);
506 }
507
507 - if (effectTag & Passive) {
508 + if (enableHooks && effectTag & Passive) {
509 rootWithPendingPassiveEffects = finishedRoot;
510 }
511
@@ -768,7 +769,11 @@ function commitRoot(root: FiberRoot, finishedWork: Fiber): void {
769 }
770 }
771
771 - if (firstEffect !== null && rootWithPendingPassiveEffects !== null) {
772 + if (
773 + enableHooks &&
774 + firstEffect !== null &&
775 + rootWithPendingPassiveEffects !== null
776 + ) {
777 // This commit included a passive effect. These do not need to fire until
778 // after the next paint. Schedule an callback to fire them in an async
779 // event. To ensure serial execution, the callback will be flushed early if
@@ -1192,7 +1197,11 @@ function renderRoot(root: FiberRoot, isYieldy: boolean): void {
1197 flushPassiveEffects();
1198
1199 isWorking = true;
1195 - ReactCurrentOwner.currentDispatcher = Dispatcher;
1200 + if (enableHooks) {
1201 + ReactCurrentOwner.currentDispatcher = Dispatcher;
1202 + } else {
1203 + ReactCurrentOwner.currentDispatcher = DispatcherWithoutHooks;
1204 + }
1205
1206 const expirationTime = root.nextExpirationTimeToWorkOn;
1207