diff --git a/src/components/Context.ts b/src/components/Context.ts index cfba5537d..5c49d5a0e 100644 --- a/src/components/Context.ts +++ b/src/components/Context.ts @@ -1,4 +1,5 @@ import { createContext } from 'react' +import type { Context } from 'react' import type { Action, AnyAction, Store } from 'redux' import type { Subscription } from '../utils/Subscription' import { StabilityCheck } from '../hooks/useSelector' @@ -13,13 +14,31 @@ export interface ReactReduxContextValue< stabilityCheck: StabilityCheck } -export const ReactReduxContext = - /*#__PURE__*/ createContext(null as any) +let realContext: Context | null = null +function getContext() { + if (!realContext) { + realContext = createContext(null as any) + if (process.env.NODE_ENV !== 'production') { + realContext.displayName = 'ReactRedux' + } + } + return realContext +} -export type ReactReduxContextInstance = typeof ReactReduxContext +export const ReactReduxContext = /*#__PURE__*/ new Proxy( + {} as Context, + /*#__PURE__*/ new Proxy>>( + {}, + { + get(_, handler) { + const target = getContext() + // @ts-ignore + return (_target, ...args) => Reflect[handler](target, ...args) + }, + } + ) +) -if (process.env.NODE_ENV !== 'production') { - ReactReduxContext.displayName = 'ReactRedux' -} +export type ReactReduxContextInstance = typeof ReactReduxContext export default ReactReduxContext