Skip to content

Commit 5c76414

Browse files
authored
Merge pull request #2039 from reduxjs/singletonContext-by-ReactVersion
2 parents 28f0aa2 + fafce9c commit 5c76414

File tree

1 file changed

+6
-2
lines changed

1 file changed

+6
-2
lines changed

src/components/Context.ts

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import { createContext } from 'react'
1+
import { createContext, version as ReactVersion } from 'react'
22
import type { Context } from 'react'
33
import type { Action, AnyAction, Store } from 'redux'
44
import type { Subscription } from '../utils/Subscription'
@@ -15,13 +15,17 @@ export interface ReactReduxContextValue<
1515
noopCheck: CheckFrequency
1616
}
1717

18-
let realContext: Context<ReactReduxContextValue> | null = null
18+
const ContextKey = Symbol.for(`react-redux-context-${ReactVersion}`)
19+
const gT = globalThis as { [ContextKey]?: Context<ReactReduxContextValue> }
20+
1921
function getContext() {
22+
let realContext = gT[ContextKey]
2023
if (!realContext) {
2124
realContext = createContext<ReactReduxContextValue>(null as any)
2225
if (process.env.NODE_ENV !== 'production') {
2326
realContext.displayName = 'ReactRedux'
2427
}
28+
gT[ContextKey] = realContext
2529
}
2630
return realContext
2731
}

0 commit comments

Comments
 (0)