Skip to content

Commit

Permalink
Enable hooks! (#14679)
Browse files Browse the repository at this point in the history
* Turned enableHooks feature flag on everywhere
* Removed useHooks feature flag from tests (now that it's on by default)
* Remove useHooks feature flag entirely
  • Loading branch information
bvaughn authored Jan 23, 2019
1 parent 73962c3 commit 6cb2677
Show file tree
Hide file tree
Showing 27 changed files with 32 additions and 107 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -16,9 +16,6 @@ let ReactDebugTools;
describe('ReactHooksInspection', () => {
beforeEach(() => {
jest.resetModules();
let ReactFeatureFlags = require('shared/ReactFeatureFlags');
// TODO: Switch this test to non-internal once the flag is on by default.
ReactFeatureFlags.enableHooks = true;
React = require('react');
ReactDebugTools = require('react-debug-tools');
});
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,9 +17,6 @@ let ReactDebugTools;
describe('ReactHooksInspectionIntergration', () => {
beforeEach(() => {
jest.resetModules();
let ReactFeatureFlags = require('shared/ReactFeatureFlags');
// TODO: Switch this test to non-internal once the flag is on by default.
ReactFeatureFlags.enableHooks = true;
React = require('react');
ReactTestRenderer = require('react-test-renderer');
ReactDebugTools = require('react-debug-tools');
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,6 @@ function initModules() {

ReactFeatureFlags = require('shared/ReactFeatureFlags');
ReactFeatureFlags.debugRenderPhaseSideEffectsForStrictMode = false;
ReactFeatureFlags.enableHooks = true;
React = require('react');
ReactDOM = require('react-dom');
ReactDOMServer = require('react-dom/server');
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@

'use strict';

let ReactFeatureFlags;
let React;
let ReactDOM;
let Suspense;
Expand All @@ -21,8 +20,6 @@ describe('ReactDOMSuspensePlaceholder', () => {

beforeEach(() => {
jest.resetModules();
ReactFeatureFlags = require('shared/ReactFeatureFlags');
ReactFeatureFlags.enableHooks = true;
React = require('react');
ReactDOM = require('react-dom');
ReactCache = require('react-cache');
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,6 @@ describe('ReactErrorBoundaries', () => {
jest.resetModules();
PropTypes = require('prop-types');
ReactFeatureFlags = require('shared/ReactFeatureFlags');
ReactFeatureFlags.enableHooks = true;
ReactFeatureFlags.replayFailedUnitOfWorkWithInvokeGuardedCallback = false;
ReactDOM = require('react-dom');
React = require('react');
Expand Down
8 changes: 1 addition & 7 deletions packages/react-dom/src/server/ReactPartialRenderer.js
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,6 @@ import describeComponentFrame from 'shared/describeComponentFrame';
import ReactSharedInternals from 'shared/ReactSharedInternals';
import {
warnAboutDeprecatedLifecycles,
enableHooks,
enableSuspenseServerRenderer,
} from 'shared/ReactFeatureFlags';

Expand Down Expand Up @@ -55,7 +54,6 @@ import {
prepareToUseHooks,
finishHooks,
Dispatcher,
DispatcherWithoutHooks,
currentThreadID,
setCurrentThreadID,
} from './ReactPartialRendererHooks';
Expand Down Expand Up @@ -786,11 +784,7 @@ class ReactDOMServerRenderer {
const prevThreadID = currentThreadID;
setCurrentThreadID(this.threadID);
const prevDispatcher = ReactCurrentDispatcher.current;
if (enableHooks) {
ReactCurrentDispatcher.current = Dispatcher;
} else {
ReactCurrentDispatcher.current = DispatcherWithoutHooks;
}
ReactCurrentDispatcher.current = Dispatcher;
try {
// Markup generated within <Suspense> ends up buffered until we know
// nothing in that boundary suspended
Expand Down
3 changes: 0 additions & 3 deletions packages/react-dom/src/server/ReactPartialRendererHooks.js
Original file line number Diff line number Diff line change
Expand Up @@ -423,6 +423,3 @@ export const Dispatcher: DispatcherType = {
// Debugging effect
useDebugValue: noop,
};
export const DispatcherWithoutHooks = {
readContext,
};
4 changes: 0 additions & 4 deletions packages/react-reconciler/src/ReactFiberCommitWork.js
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,6 @@ import type {Thenable} from './ReactFiberScheduler';

import {unstable_wrap as Schedule_tracing_wrap} from 'scheduler/tracing';
import {
enableHooks,
enableSchedulerTracing,
enableProfilerTimer,
} from 'shared/ReactFeatureFlags';
Expand Down Expand Up @@ -312,9 +311,6 @@ 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: 0 additions & 3 deletions packages/react-reconciler/src/ReactFiberDispatcher.js
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,3 @@ export const Dispatcher = {
useRef,
useState,
};
export const DispatcherWithoutHooks = {
readContext,
};
7 changes: 0 additions & 7 deletions packages/react-reconciler/src/ReactFiberHooks.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,6 @@ import type {ExpirationTime} from './ReactFiberExpirationTime';
import type {HookEffectTag} from './ReactHookEffectTags';

import {NoWork} from './ReactFiberExpirationTime';
import {enableHooks} from 'shared/ReactFeatureFlags';
import {
readContext,
stashContextDependencies,
Expand Down Expand Up @@ -296,9 +295,6 @@ export function renderWithHooks(
refOrContext: any,
nextRenderExpirationTime: ExpirationTime,
): any {
if (!enableHooks) {
return Component(props, refOrContext);
}
renderExpirationTime = nextRenderExpirationTime;
currentlyRenderingFiber = workInProgress;
firstCurrentHook = current !== null ? current.memoizedState : null;
Expand Down Expand Up @@ -397,9 +393,6 @@ export function bailoutHooks(
}

export function resetHooks(): void {
if (!enableHooks) {
return;
}
if (__DEV__) {
flushHookMismatchWarnings();
}
Expand Down
17 changes: 4 additions & 13 deletions packages/react-reconciler/src/ReactFiberScheduler.js
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,6 @@ import {
SimpleMemoComponent,
} from 'shared/ReactWorkTags';
import {
enableHooks,
enableSchedulerTracing,
enableProfilerTimer,
enableUserTimingAPI,
Expand Down Expand Up @@ -165,7 +164,7 @@ import {
commitDetachRef,
commitPassiveHookEffects,
} from './ReactFiberCommitWork';
import {Dispatcher, DispatcherWithoutHooks} from './ReactFiberDispatcher';
import {Dispatcher} from './ReactFiberDispatcher';

export type Thenable = {
then(resolve: () => mixed, reject?: () => mixed): mixed,
Expand Down Expand Up @@ -510,7 +509,7 @@ function commitAllLifeCycles(
commitAttachRef(nextEffect);
}

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

Expand Down Expand Up @@ -784,11 +783,7 @@ function commitRoot(root: FiberRoot, finishedWork: Fiber): void {
}
}

if (
enableHooks &&
firstEffect !== null &&
rootWithPendingPassiveEffects !== null
) {
if (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 @@ -1221,11 +1216,7 @@ function renderRoot(root: FiberRoot, isYieldy: boolean): void {
flushPassiveEffects();

isWorking = true;
if (enableHooks) {
ReactCurrentDispatcher.current = Dispatcher;
} else {
ReactCurrentDispatcher.current = DispatcherWithoutHooks;
}
ReactCurrentDispatcher.current = Dispatcher;

const expirationTime = root.nextExpirationTimeToWorkOn;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,6 @@ describe('ReactHooks', () => {

ReactFeatureFlags = require('shared/ReactFeatureFlags');
ReactFeatureFlags.debugRenderPhaseSideEffectsForStrictMode = false;
ReactFeatureFlags.enableHooks = true;
React = require('react');
ReactTestRenderer = require('react-test-renderer');
ReactDOMServer = require('react-dom/server');
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,6 @@ describe('ReactHooksWithNoopRenderer', () => {

ReactFeatureFlags = require('shared/ReactFeatureFlags');
ReactFeatureFlags.debugRenderPhaseSideEffectsForStrictMode = false;
ReactFeatureFlags.enableHooks = true;
ReactFeatureFlags.enableSchedulerTracing = true;
React = require('react');
ReactNoop = require('react-noop-renderer');
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,6 @@ describe('ReactNewContext', () => {
jest.resetModules();
ReactFeatureFlags = require('shared/ReactFeatureFlags');
ReactFeatureFlags.debugRenderPhaseSideEffectsForStrictMode = false;
ReactFeatureFlags.enableHooks = true;
React = require('react');
useContext = React.useContext;
ReactNoop = require('react-noop-renderer');
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,6 @@ describe('ReactSuspense', () => {
ReactFeatureFlags = require('shared/ReactFeatureFlags');
ReactFeatureFlags.debugRenderPhaseSideEffectsForStrictMode = false;
ReactFeatureFlags.replayFailedUnitOfWorkWithInvokeGuardedCallback = false;
ReactFeatureFlags.enableHooks = true;
React = require('react');
ReactTestRenderer = require('react-test-renderer');
// JestReact = require('jest-react');
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,6 @@ describe('ReactSuspenseFuzz', () => {
ReactFeatureFlags = require('shared/ReactFeatureFlags');
ReactFeatureFlags.debugRenderPhaseSideEffectsForStrictMode = false;
ReactFeatureFlags.replayFailedUnitOfWorkWithInvokeGuardedCallback = false;
ReactFeatureFlags.enableHooks = true;
React = require('react');
Suspense = React.Suspense;
ReactTestRenderer = require('react-test-renderer');
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,6 @@ describe('ReactSuspenseWithNoopRenderer', () => {
beforeEach(() => {
jest.resetModules();
ReactFeatureFlags = require('shared/ReactFeatureFlags');
ReactFeatureFlags.enableHooks = true;
ReactFeatureFlags.debugRenderPhaseSideEffectsForStrictMode = false;
ReactFeatureFlags.replayFailedUnitOfWorkWithInvokeGuardedCallback = false;
React = require('react');
Expand Down
43 changes: 16 additions & 27 deletions packages/react-test-renderer/src/ReactShallowRenderer.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,6 @@ import shallowEqual from 'shared/shallowEqual';
import invariant from 'shared/invariant';
import checkPropTypes from 'prop-types/checkPropTypes';
import ReactSharedInternals from 'shared/ReactSharedInternals';
import {enableHooks} from 'shared/ReactFeatureFlags';
import warning from 'shared/warning';
import is from 'shared/objectIs';

Expand Down Expand Up @@ -187,17 +186,15 @@ class ReactShallowRenderer {
this._rendering = false;
this._forcedUpdate = false;
this._updater = new Updater(this);
if (enableHooks) {
this._dispatcher = this._createDispatcher();
this._workInProgressHook = null;
this._firstWorkInProgressHook = null;
this._isReRender = false;
this._didScheduleRenderPhaseUpdate = false;
this._renderPhaseUpdates = null;
this._currentlyRenderingComponent = null;
this._numberOfReRenders = 0;
this._previousComponentIdentity = null;
}
this._dispatcher = this._createDispatcher();
this._workInProgressHook = null;
this._firstWorkInProgressHook = null;
this._isReRender = false;
this._didScheduleRenderPhaseUpdate = false;
this._renderPhaseUpdates = null;
this._currentlyRenderingComponent = null;
this._numberOfReRenders = 0;
this._previousComponentIdentity = null;
}

_context: null | Object;
Expand Down Expand Up @@ -560,27 +557,19 @@ class ReactShallowRenderer {

this._mountClassComponent(element, this._context);
} else {
if (enableHooks) {
const prevDispatcher = ReactCurrentDispatcher.current;
ReactCurrentDispatcher.current = this._dispatcher;
this._prepareToUseHooks(element.type);
try {
this._rendered = element.type.call(
undefined,
element.props,
this._context,
);
} finally {
ReactCurrentDispatcher.current = prevDispatcher;
}
this._finishHooks(element, context);
} else {
const prevDispatcher = ReactCurrentDispatcher.current;
ReactCurrentDispatcher.current = this._dispatcher;
this._prepareToUseHooks(element.type);
try {
this._rendered = element.type.call(
undefined,
element.props,
this._context,
);
} finally {
ReactCurrentDispatcher.current = prevDispatcher;
}
this._finishHooks(element, context);
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,9 +16,6 @@ let React;
describe('ReactShallowRenderer with hooks', () => {
beforeEach(() => {
jest.resetModules();
let ReactFeatureFlags = require('shared/ReactFeatureFlags');
// TODO: Switch this test to non-internal once the flag is on by default.
ReactFeatureFlags.enableHooks = true;
createRenderer = require('react-test-renderer/shallow').createRenderer;
React = require('react');
});
Expand Down
25 changes: 11 additions & 14 deletions packages/react/src/React.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,6 @@ import {
REACT_STRICT_MODE_TYPE,
REACT_SUSPENSE_TYPE,
} from 'shared/ReactSymbols';
import {enableHooks} from 'shared/ReactFeatureFlags';

import {Component, PureComponent} from './ReactBaseClasses';
import {createRef} from './ReactCreateRef';
Expand Down Expand Up @@ -66,6 +65,17 @@ const React = {
lazy,
memo,

useCallback,
useContext,
useEffect,
useImperativeHandle,
useDebugValue,
useLayoutEffect,
useMemo,
useReducer,
useRef,
useState,

Fragment: REACT_FRAGMENT_TYPE,
StrictMode: REACT_STRICT_MODE_TYPE,
Suspense: REACT_SUSPENSE_TYPE,
Expand Down Expand Up @@ -95,17 +105,4 @@ if (enableStableConcurrentModeAPIs) {
React.unstable_Profiler = undefined;
}

if (enableHooks) {
React.useCallback = useCallback;
React.useContext = useContext;
React.useEffect = useEffect;
React.useImperativeHandle = useImperativeHandle;
React.useDebugValue = useDebugValue;
React.useLayoutEffect = useLayoutEffect;
React.useMemo = useMemo;
React.useReducer = useReducer;
React.useRef = useRef;
React.useState = useState;
}

export default React;
1 change: 0 additions & 1 deletion packages/shared/ReactFeatureFlags.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@

export const enableUserTimingAPI = __DEV__;

export const enableHooks = false;
// Helps identify side effects in begin-phase lifecycle hooks and setState reducers:
export const debugRenderPhaseSideEffects = false;

Expand Down
1 change: 0 additions & 1 deletion packages/shared/forks/ReactFeatureFlags.native-fb.js
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,6 @@ import typeof * as FeatureFlagsShimType from './ReactFeatureFlags.native-fb';
export const {debugRenderPhaseSideEffects} = require('ReactFeatureFlags');

// The rest of the flags are static for better dead code elimination.
export const enableHooks = true;
export const enableUserTimingAPI = __DEV__;
export const enableProfilerTimer = __PROFILE__;
export const enableSchedulerTracing = __PROFILE__;
Expand Down
1 change: 0 additions & 1 deletion packages/shared/forks/ReactFeatureFlags.native-oss.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,6 @@ import typeof * as FeatureFlagsShimType from './ReactFeatureFlags.native-oss';

export const debugRenderPhaseSideEffects = false;
export const debugRenderPhaseSideEffectsForStrictMode = false;
export const enableHooks = false;
export const enableUserTimingAPI = __DEV__;
export const replayFailedUnitOfWorkWithInvokeGuardedCallback = __DEV__;
export const warnAboutDeprecatedLifecycles = false;
Expand Down
1 change: 0 additions & 1 deletion packages/shared/forks/ReactFeatureFlags.persistent.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,6 @@ import typeof * as PersistentFeatureFlagsType from './ReactFeatureFlags.persiste
export const debugRenderPhaseSideEffects = false;
export const debugRenderPhaseSideEffectsForStrictMode = false;
export const enableUserTimingAPI = __DEV__;
export const enableHooks = false;
export const warnAboutDeprecatedLifecycles = false;
export const replayFailedUnitOfWorkWithInvokeGuardedCallback = __DEV__;
export const enableProfilerTimer = __PROFILE__;
Expand Down
Loading

0 comments on commit 6cb2677

Please sign in to comment.