/** * Copyright (c) Meta Platforms, Inc. and affiliates. * * This source code is licensed under the MIT license found in the * LICENSE file in the root directory of this source tree. * * @flow */ import type {Dispatcher} from 'react-reconciler/src/ReactInternalTypes'; import type { ReactContext, StartTransitionOptions, Usable, Awaited, } from 'shared/ReactTypes'; import {REACT_CONSUMER_TYPE} from 'shared/ReactSymbols'; import ReactSharedInternals from 'shared/ReactSharedInternals'; type BasicStateAction = (S => S) | S; type Dispatch = A => void; function resolveDispatcher() { const dispatcher = ReactSharedInternals.H; if (__DEV__) { if (dispatcher === null) { console.error( 'Invalid hook call. Hooks can only be called inside of the body of a function component. This could happen for' + ' one of the following reasons:\n' + '1. You might have mismatching versions of React and the renderer (such as React DOM)\n' + '2. You might be breaking the Rules of Hooks\n' + '3. You might have more than one copy of React in the same app\n' + 'See https://react.dev/link/invalid-hook-call for tips about how to debug and fix this problem.', ); } } // Will result in a null access error if accessed outside render phase. We // intentionally don't throw our own error because this is in a hot path. // Also helps ensure this is inlined. return ((dispatcher: any): Dispatcher); } export function getCacheForType(resourceType: () => T): T { const dispatcher = ReactSharedInternals.A; if (!dispatcher) { // If there is no dispatcher, then we treat this as not being cached. return resourceType(); } return dispatcher.getCacheForType(resourceType); } export function useContext(Context: ReactContext): T { const dispatcher = resolveDispatcher(); if (__DEV__) { if (Context.$$typeof === REACT_CONSUMER_TYPE) { console.error( 'Calling useContext(Context.Consumer) is not supported and will cause bugs. ' + 'Did you mean to call useContext(Context) instead?', ); } } return dispatcher.useContext(Context); } export function useState( initialState: (() => S) | S, ): [S, Dispatch>] { const dispatcher = resolveDispatcher(); return dispatcher.useState(initialState); } export function useReducer( reducer: (S, A) => S, initialArg: I, init?: I => S, ): [S, Dispatch] { const dispatcher = resolveDispatcher(); return dispatcher.useReducer(reducer, initialArg, init); } export function useRef(initialValue: T): {current: T} { const dispatcher = resolveDispatcher(); return dispatcher.useRef(initialValue); } export function useEffect( create: () => (() => void) | void, deps: Array | void | null, ): void { if (__DEV__) { if (create == null) { console.warn( 'React Hook useEffect requires an effect callback. Did you forget to pass a callback to the hook?', ); } } const dispatcher = resolveDispatcher(); return dispatcher.useEffect(create, deps); } export function useInsertionEffect( create: () => (() => void) | void, deps: Array | void | null, ): void { if (__DEV__) { if (create == null) { console.warn( 'React Hook useInsertionEffect requires an effect callback. Did you forget to pass a callback to the hook?', ); } } const dispatcher = resolveDispatcher(); return dispatcher.useInsertionEffect(create, deps); } export function useLayoutEffect( create: () => (() => void) | void, deps: Array | void | null, ): void { if (__DEV__) { if (create == null) { console.warn( 'React Hook useLayoutEffect requires an effect callback. Did you forget to pass a callback to the hook?', ); } } const dispatcher = resolveDispatcher(); return dispatcher.useLayoutEffect(create, deps); } export function useCallback( callback: T, deps: Array | void | null, ): T { const dispatcher = resolveDispatcher(); return dispatcher.useCallback(callback, deps); } export function useMemo( create: () => T, deps: Array | void | null, ): T { const dispatcher = resolveDispatcher(); return dispatcher.useMemo(create, deps); } export function useImperativeHandle( ref: {current: T | null} | ((inst: T | null) => mixed) | null | void, create: () => T, deps: Array | void | null, ): void { const dispatcher = resolveDispatcher(); return dispatcher.useImperativeHandle(ref, create, deps); } export function useDebugValue( value: T, formatterFn: ?(value: T) => mixed, ): void { if (__DEV__) { const dispatcher = resolveDispatcher(); return dispatcher.useDebugValue(value, formatterFn); } } export function useTransition(): [ boolean, (callback: () => void, options?: StartTransitionOptions) => void, ] { const dispatcher = resolveDispatcher(); return dispatcher.useTransition(); } export function useDeferredValue(value: T, initialValue?: T): T { const dispatcher = resolveDispatcher(); return dispatcher.useDeferredValue(value, initialValue); } export function useId(): string { const dispatcher = resolveDispatcher(); return dispatcher.useId(); } export function useSyncExternalStore( subscribe: (() => void) => () => void, getSnapshot: () => T, getServerSnapshot?: () => T, ): T { const dispatcher = resolveDispatcher(); return dispatcher.useSyncExternalStore( subscribe, getSnapshot, getServerSnapshot, ); } export function useCacheRefresh(): (?() => T, ?T) => void { const dispatcher = resolveDispatcher(); // $FlowFixMe[not-a-function] This is unstable, thus optional return dispatcher.useCacheRefresh(); } export function use(usable: Usable): T { const dispatcher = resolveDispatcher(); return dispatcher.use(usable); } export function useMemoCache(size: number): Array { const dispatcher = resolveDispatcher(); // $FlowFixMe[not-a-function] This is unstable, thus optional return dispatcher.useMemoCache(size); } export function useEffectEvent) => mixed>( callback: F, ): F { const dispatcher = resolveDispatcher(); // $FlowFixMe[not-a-function] This is unstable, thus optional return dispatcher.useEffectEvent(callback); } export function useOptimistic( passthrough: S, reducer: ?(S, A) => S, ): [S, (A) => void] { const dispatcher = resolveDispatcher(); return dispatcher.useOptimistic(passthrough, reducer); } export function useActionState( action: (Awaited, P) => S, initialState: Awaited, permalink?: string, ): [Awaited, (P) => void, boolean] { const dispatcher = resolveDispatcher(); return dispatcher.useActionState(action, initialState, permalink); }