From fafce9cbd3b91cb8b61482bc167f024f39843c22 Mon Sep 17 00:00:00 2001 From: Lenz Weber Date: Tue, 20 Jun 2023 09:00:06 +0200 Subject: [PATCH] create singleton context by React version --- src/components/Context.ts | 8 ++++++-- 1 file changed, 6 insertions(+), 2 deletions(-) diff --git a/src/components/Context.ts b/src/components/Context.ts index 8454e09f1..72e3fb10e 100644 --- a/src/components/Context.ts +++ b/src/components/Context.ts @@ -1,4 +1,4 @@ -import { createContext } from 'react' +import { createContext, version as ReactVersion } from 'react' import type { Context } from 'react' import type { Action, AnyAction, Store } from 'redux' import type { Subscription } from '../utils/Subscription' @@ -15,13 +15,17 @@ export interface ReactReduxContextValue< noopCheck: CheckFrequency } -let realContext: Context | null = null +const ContextKey = Symbol.for(`react-redux-context-${ReactVersion}`) +const gT = globalThis as { [ContextKey]?: Context } + function getContext() { + let realContext = gT[ContextKey] if (!realContext) { realContext = createContext(null as any) if (process.env.NODE_ENV !== 'production') { realContext.displayName = 'ReactRedux' } + gT[ContextKey] = realContext } return realContext }