/
githubmr
/
facebook-react
Обзор
Документация
Войти
/
githubmr
/
facebook-react
Код
Запросы
0
Задачи
Вики
Пакеты
0
Релизы
0
Аналитика
v18.0.0
packages/react-server/src/ReactFlightHooks.js
93 строки
3 KB
salazarm
[ServerContext] Flight support for ServerContext (#23244)
08 мар 2022, 15:55
Не верифицирован
08 мар 2022, 15:55
d5f1b06
Код
Авторство
О чём код?
/** * Copyright (c) Facebook, Inc. and its 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 as DispatcherType} from 'react-reconciler/src/ReactInternalTypes'; import type {ReactServerContext} from 'shared/ReactTypes'; import {REACT_SERVER_CONTEXT_TYPE} from 'shared/ReactSymbols'; import {readContext as readContextImpl} from './ReactFlightNewContext'; function readContext<T>(context: ReactServerContext<T>): T { if (__DEV__) { if (context.$$typeof !== REACT_SERVER_CONTEXT_TYPE) { console.error('Only ServerContext is supported in Flight'); } if (currentCache === null) { console.error( 'Context can only be read while React is rendering. ' + 'In classes, you can read it in the render method or getDerivedStateFromProps. ' + 'In function components, you can read it directly in the function body, but not ' + 'inside Hooks like useReducer() or useMemo().', ); } } return readContextImpl(context); } export const Dispatcher: DispatcherType = { useMemo<T>(nextCreate: () => T): T { return nextCreate(); }, useCallback<T>(callback: T): T { return callback; }, useDebugValue(): void {}, useDeferredValue: (unsupportedHook: any), useTransition: (unsupportedHook: any), getCacheForType<T>(resourceType: () => T): T { if (!currentCache) { throw new Error('Reading the cache is only supported while rendering.'); } let entry: T | void = (currentCache.get(resourceType): any); if (entry === undefined) { entry = resourceType(); // TODO: Warn if undefined? currentCache.set(resourceType, entry); } return entry; }, readContext, useContext: readContext, useReducer: (unsupportedHook: any), useRef: (unsupportedHook: any), useState: (unsupportedHook: any), useInsertionEffect: (unsupportedHook: any), useLayoutEffect: (unsupportedHook: any), useImperativeHandle: (unsupportedHook: any), useEffect: (unsupportedHook: any), useId: (unsupportedHook: any), useMutableSource: (unsupportedHook: any), useSyncExternalStore: (unsupportedHook: any), useCacheRefresh(): <T>(?() => T, ?T) => void { return unsupportedRefresh; }, }; function unsupportedHook(): void { throw new Error('This Hook is not supported in Server Components.'); } function unsupportedRefresh(): void { if (!currentCache) { throw new Error( 'Refreshing the cache is not supported in Server Components.', ); } } let currentCache: Map<Function, mixed> | null = null; export function setCurrentCache(cache: Map<Function, mixed> | null) { currentCache = cache; return currentCache; } export function getCurrentCache() { return currentCache; }