Skip to content

Commit

Permalink
DCE hooks code when flag is off (facebook#14111)
Browse files Browse the repository at this point in the history
  • Loading branch information
sophiebits authored and jetoneza committed Jan 23, 2019
1 parent 0a18522 commit 863541e
Show file tree
Hide file tree
Showing 6 changed files with 42 additions and 5 deletions.
8 changes: 7 additions & 1 deletion packages/react-dom/src/server/ReactPartialRenderer.js
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ import describeComponentFrame from 'shared/describeComponentFrame';
import ReactSharedInternals from 'shared/ReactSharedInternals';
import {
warnAboutDeprecatedLifecycles,
enableHooks,
enableSuspenseServerRenderer,
} from 'shared/ReactFeatureFlags';

Expand Down Expand Up @@ -52,6 +53,7 @@ import {
prepareToUseHooks,
finishHooks,
Dispatcher,
DispatcherWithoutHooks,
} from './ReactPartialRendererHooks';
import {
Namespaces,
Expand Down Expand Up @@ -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) {
Expand Down
3 changes: 3 additions & 0 deletions packages/react-dom/src/server/ReactPartialRendererHooks.js
Original file line number Diff line number Diff line change
Expand Up @@ -350,3 +350,6 @@ export const Dispatcher = {
// Effects are not run in the server environment.
useEffect: noop,
};
export const DispatcherWithoutHooks = {
readContext,
};
4 changes: 4 additions & 0 deletions packages/react-reconciler/src/ReactFiberCommitWork.js
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ import type {SuspenseState} from './ReactFiberSuspenseComponent';
import type {FunctionComponentUpdateQueue} from './ReactFiberHooks';

import {
enableHooks,
enableSchedulerTracing,
enableProfilerTimer,
} from 'shared/ReactFeatureFlags';
Expand Down Expand Up @@ -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) {
Expand Down
3 changes: 3 additions & 0 deletions packages/react-reconciler/src/ReactFiberDispatcher.js
Original file line number Diff line number Diff line change
Expand Up @@ -34,3 +34,6 @@ export const Dispatcher = {
useRef,
useState,
};
export const DispatcherWithoutHooks = {
readContext,
};
12 changes: 12 additions & 0 deletions packages/react-reconciler/src/ReactFiberHooks.js
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down Expand Up @@ -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;
Expand All @@ -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.

Expand Down Expand Up @@ -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.
Expand Down
17 changes: 13 additions & 4 deletions packages/react-reconciler/src/ReactFiberScheduler.js
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,7 @@ import {
SimpleMemoComponent,
} from 'shared/ReactWorkTags';
import {
enableHooks,
enableSchedulerTracing,
enableProfilerTimer,
enableUserTimingAPI,
Expand Down Expand Up @@ -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,
Expand Down Expand Up @@ -504,7 +505,7 @@ function commitAllLifeCycles(
commitAttachRef(nextEffect);
}

if (effectTag & Passive) {
if (enableHooks && effectTag & Passive) {
rootWithPendingPassiveEffects = finishedRoot;
}

Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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;

Expand Down

0 comments on commit 863541e

Please sign in to comment.