Skip to content

Clean up enableUseHook flag #26707

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Apr 23, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 0 additions & 2 deletions packages/react-client/src/__tests__/ReactFlight-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -530,7 +530,6 @@ describe('ReactFlight', () => {
expect(ReactNoop).toMatchRenderedOutput(<div>I am client</div>);
});

// @gate enableUseHook
it('should error if a non-serializable value is passed to a host component', async () => {
function ClientImpl({children}) {
return children;
Expand Down Expand Up @@ -641,7 +640,6 @@ describe('ReactFlight', () => {
});
});

// @gate enableUseHook
it('should trigger the inner most error boundary inside a Client Component', async () => {
function ServerComponent() {
throw new Error('This was thrown in the Server Component.');
Expand Down
8 changes: 8 additions & 0 deletions packages/react-debug-tools/src/ReactDebugHooks.js
Original file line number Diff line number Diff line change
Expand Up @@ -106,6 +106,13 @@ function readContext<T>(context: ReactContext<T>): T {
return context._currentValue;
}

function use<T>(): T {
// TODO: What should this do if it receives an unresolved promise?
throw new Error(
'Support for `use` not yet implemented in react-debug-tools.',
);
}

function useContext<T>(context: ReactContext<T>): T {
hookLog.push({
primitive: 'Context',
Expand Down Expand Up @@ -327,6 +334,7 @@ function useId(): string {
}

const Dispatcher: DispatcherType = {
use,
readContext,
useCacheRefresh,
useCallback,
Expand Down
6 changes: 0 additions & 6 deletions packages/react-dom/src/__tests__/ReactDOMFizzServer-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -5212,7 +5212,6 @@ describe('ReactDOMFizzServer', () => {
});
});

// @gate enableUseHook
it('basic use(promise)', async () => {
const promiseA = Promise.resolve('A');
const promiseB = Promise.resolve('B');
Expand Down Expand Up @@ -5258,7 +5257,6 @@ describe('ReactDOMFizzServer', () => {
expect(getVisibleChildren(container)).toEqual('ABC');
});

// @gate enableUseHook
it('basic use(context)', async () => {
const ContextA = React.createContext('default');
const ContextB = React.createContext('B');
Expand Down Expand Up @@ -5303,7 +5301,6 @@ describe('ReactDOMFizzServer', () => {
expect(getVisibleChildren(container)).toEqual(['AB', 'C']);
});

// @gate enableUseHook
it('use(promise) in multiple components', async () => {
const promiseA = Promise.resolve('A');
const promiseB = Promise.resolve('B');
Expand Down Expand Up @@ -5357,7 +5354,6 @@ describe('ReactDOMFizzServer', () => {
expect(getVisibleChildren(container)).toEqual('ABCD');
});

// @gate enableUseHook
it('using a rejected promise will throw', async () => {
const promiseA = Promise.resolve('A');
const promiseB = Promise.reject(new Error('Oops!'));
Expand Down Expand Up @@ -5443,7 +5439,6 @@ describe('ReactDOMFizzServer', () => {
}
});

// @gate enableUseHook
it("use a promise that's already been instrumented and resolved", async () => {
const thenable = {
status: 'fulfilled',
Expand All @@ -5467,7 +5462,6 @@ describe('ReactDOMFizzServer', () => {
expect(getVisibleChildren(container)).toEqual('Hi');
});

// @gate enableUseHook
it('unwraps thenable that fulfills synchronously without suspending', async () => {
function App() {
const thenable = {
Expand Down
71 changes: 20 additions & 51 deletions packages/react-reconciler/src/ReactFiberHooks.js
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,6 @@ import {
enableLazyContextPropagation,
enableUseMutableSource,
enableTransitionTracing,
enableUseHook,
enableUseMemoCacheHook,
enableUseEffectEventHook,
enableLegacyCache,
Expand Down Expand Up @@ -2944,6 +2943,7 @@ function markUpdateInDevTools<A>(fiber: Fiber, lane: Lane, action: A): void {
export const ContextOnlyDispatcher: Dispatcher = {
readContext,

use,
useCallback: throwInvalidHookError,
useContext: throwInvalidHookError,
useEffect: throwInvalidHookError,
Expand All @@ -2964,9 +2964,6 @@ export const ContextOnlyDispatcher: Dispatcher = {
if (enableCache) {
(ContextOnlyDispatcher: Dispatcher).useCacheRefresh = throwInvalidHookError;
}
if (enableUseHook) {
(ContextOnlyDispatcher: Dispatcher).use = throwInvalidHookError;
}
if (enableUseMemoCacheHook) {
(ContextOnlyDispatcher: Dispatcher).useMemoCache = throwInvalidHookError;
}
Expand All @@ -2977,6 +2974,7 @@ if (enableUseEffectEventHook) {
const HooksDispatcherOnMount: Dispatcher = {
readContext,

use,
useCallback: mountCallback,
useContext: readContext,
useEffect: mountEffect,
Expand All @@ -2997,9 +2995,6 @@ const HooksDispatcherOnMount: Dispatcher = {
if (enableCache) {
(HooksDispatcherOnMount: Dispatcher).useCacheRefresh = mountRefresh;
}
if (enableUseHook) {
(HooksDispatcherOnMount: Dispatcher).use = use;
}
if (enableUseMemoCacheHook) {
(HooksDispatcherOnMount: Dispatcher).useMemoCache = useMemoCache;
}
Expand All @@ -3009,6 +3004,7 @@ if (enableUseEffectEventHook) {
const HooksDispatcherOnUpdate: Dispatcher = {
readContext,

use,
useCallback: updateCallback,
useContext: readContext,
useEffect: updateEffect,
Expand All @@ -3032,16 +3028,14 @@ if (enableCache) {
if (enableUseMemoCacheHook) {
(HooksDispatcherOnUpdate: Dispatcher).useMemoCache = useMemoCache;
}
if (enableUseHook) {
(HooksDispatcherOnUpdate: Dispatcher).use = use;
}
if (enableUseEffectEventHook) {
(HooksDispatcherOnUpdate: Dispatcher).useEffectEvent = updateEvent;
}

const HooksDispatcherOnRerender: Dispatcher = {
readContext,

use,
useCallback: updateCallback,
useContext: readContext,
useEffect: updateEffect,
Expand All @@ -3062,9 +3056,6 @@ const HooksDispatcherOnRerender: Dispatcher = {
if (enableCache) {
(HooksDispatcherOnRerender: Dispatcher).useCacheRefresh = updateRefresh;
}
if (enableUseHook) {
(HooksDispatcherOnRerender: Dispatcher).use = use;
}
if (enableUseMemoCacheHook) {
(HooksDispatcherOnRerender: Dispatcher).useMemoCache = useMemoCache;
}
Expand Down Expand Up @@ -3103,6 +3094,7 @@ if (__DEV__) {
readContext<T>(context: ReactContext<T>): T {
return readContext(context);
},
use,
useCallback<T>(callback: T, deps: Array<mixed> | void | null): T {
currentHookNameInDev = 'useCallback';
mountHookTypesDev();
Expand Down Expand Up @@ -3243,9 +3235,6 @@ if (__DEV__) {
return mountRefresh();
};
}
if (enableUseHook) {
(HooksDispatcherOnMountInDEV: Dispatcher).use = use;
}
if (enableUseMemoCacheHook) {
(HooksDispatcherOnMountInDEV: Dispatcher).useMemoCache = useMemoCache;
}
Expand All @@ -3264,6 +3253,7 @@ if (__DEV__) {
readContext<T>(context: ReactContext<T>): T {
return readContext(context);
},
use,
useCallback<T>(callback: T, deps: Array<mixed> | void | null): T {
currentHookNameInDev = 'useCallback';
updateHookTypesDev();
Expand Down Expand Up @@ -3398,9 +3388,6 @@ if (__DEV__) {
return mountRefresh();
};
}
if (enableUseHook) {
(HooksDispatcherOnMountWithHookTypesInDEV: Dispatcher).use = use;
}
if (enableUseMemoCacheHook) {
(HooksDispatcherOnMountWithHookTypesInDEV: Dispatcher).useMemoCache =
useMemoCache;
Expand All @@ -3420,6 +3407,7 @@ if (__DEV__) {
readContext<T>(context: ReactContext<T>): T {
return readContext(context);
},
use,
useCallback<T>(callback: T, deps: Array<mixed> | void | null): T {
currentHookNameInDev = 'useCallback';
updateHookTypesDev();
Expand Down Expand Up @@ -3557,9 +3545,6 @@ if (__DEV__) {
return updateRefresh();
};
}
if (enableUseHook) {
(HooksDispatcherOnUpdateInDEV: Dispatcher).use = use;
}
if (enableUseMemoCacheHook) {
(HooksDispatcherOnUpdateInDEV: Dispatcher).useMemoCache = useMemoCache;
}
Expand All @@ -3578,7 +3563,7 @@ if (__DEV__) {
readContext<T>(context: ReactContext<T>): T {
return readContext(context);
},

use,
useCallback<T>(callback: T, deps: Array<mixed> | void | null): T {
currentHookNameInDev = 'useCallback';
updateHookTypesDev();
Expand Down Expand Up @@ -3716,9 +3701,6 @@ if (__DEV__) {
return updateRefresh();
};
}
if (enableUseHook) {
(HooksDispatcherOnRerenderInDEV: Dispatcher).use = use;
}
if (enableUseMemoCacheHook) {
(HooksDispatcherOnRerenderInDEV: Dispatcher).useMemoCache = useMemoCache;
}
Expand All @@ -3738,6 +3720,10 @@ if (__DEV__) {
warnInvalidContextAccess();
return readContext(context);
},
use<T>(usable: Usable<T>): T {
warnInvalidHookAccess();
return use(usable);
},
useCallback<T>(callback: T, deps: Array<mixed> | void | null): T {
currentHookNameInDev = 'useCallback';
warnInvalidHookAccess();
Expand Down Expand Up @@ -3888,14 +3874,6 @@ if (__DEV__) {
return mountRefresh();
};
}
if (enableUseHook) {
(InvalidNestedHooksDispatcherOnMountInDEV: Dispatcher).use = function <T>(
usable: Usable<T>,
): T {
warnInvalidHookAccess();
return use(usable);
};
}
if (enableUseMemoCacheHook) {
(InvalidNestedHooksDispatcherOnMountInDEV: Dispatcher).useMemoCache =
function (size: number): Array<any> {
Expand All @@ -3920,6 +3898,10 @@ if (__DEV__) {
warnInvalidContextAccess();
return readContext(context);
},
use<T>(usable: Usable<T>): T {
warnInvalidHookAccess();
return use(usable);
},
useCallback<T>(callback: T, deps: Array<mixed> | void | null): T {
currentHookNameInDev = 'useCallback';
warnInvalidHookAccess();
Expand Down Expand Up @@ -4073,14 +4055,6 @@ if (__DEV__) {
return updateRefresh();
};
}
if (enableUseHook) {
(InvalidNestedHooksDispatcherOnUpdateInDEV: Dispatcher).use = function <T>(
usable: Usable<T>,
): T {
warnInvalidHookAccess();
return use(usable);
};
}
if (enableUseMemoCacheHook) {
(InvalidNestedHooksDispatcherOnUpdateInDEV: Dispatcher).useMemoCache =
function (size: number): Array<any> {
Expand All @@ -4105,7 +4079,10 @@ if (__DEV__) {
warnInvalidContextAccess();
return readContext(context);
},

use<T>(usable: Usable<T>): T {
warnInvalidHookAccess();
return use(usable);
},
useCallback<T>(callback: T, deps: Array<mixed> | void | null): T {
currentHookNameInDev = 'useCallback';
warnInvalidHookAccess();
Expand Down Expand Up @@ -4259,14 +4236,6 @@ if (__DEV__) {
return updateRefresh();
};
}
if (enableUseHook) {
(InvalidNestedHooksDispatcherOnRerenderInDEV: Dispatcher).use = function <
T,
>(usable: Usable<T>): T {
warnInvalidHookAccess();
return use(usable);
};
}
if (enableUseMemoCacheHook) {
(InvalidNestedHooksDispatcherOnRerenderInDEV: Dispatcher).useMemoCache =
function (size: number): Array<any> {
Expand Down
2 changes: 1 addition & 1 deletion packages/react-reconciler/src/ReactInternalTypes.js
Original file line number Diff line number Diff line change
Expand Up @@ -372,7 +372,7 @@ type BasicStateAction<S> = (S => S) | S;
type Dispatch<A> = A => void;

export type Dispatcher = {
use?: <T>(Usable<T>) => T,
use: <T>(Usable<T>) => T,
readContext<T>(context: ReactContext<T>): T,
useState<S>(initialState: (() => S) | S): [S, Dispatch<BasicStateAction<S>>],
useReducer<S, I, A>(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -206,7 +206,6 @@ describe('isomorphic act()', () => {
});

// @gate __DEV__
// @gate enableUseHook
test('unwraps promises by yielding to microtasks (async act scope)', async () => {
const promise = Promise.resolve('Async');

Expand All @@ -232,7 +231,6 @@ describe('isomorphic act()', () => {
});

// @gate __DEV__
// @gate enableUseHook
test('unwraps promises by yielding to microtasks (non-async act scope)', async () => {
const promise = Promise.resolve('Async');

Expand Down Expand Up @@ -260,7 +258,6 @@ describe('isomorphic act()', () => {
});

// @gate __DEV__
// @gate enableUseHook
test('warns if a promise is used in a non-awaited `act` scope', async () => {
const promise = new Promise(() => {});

Expand Down
Loading