Skip to content

Commit

Permalink
Merge pull request #2039 from reduxjs/singletonContext-by-ReactVersion
Browse files Browse the repository at this point in the history
  • Loading branch information
markerikson committed Jun 21, 2023
2 parents 28f0aa2 + fafce9c commit 5c76414
Showing 1 changed file with 6 additions and 2 deletions.
8 changes: 6 additions & 2 deletions 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'
Expand All @@ -15,13 +15,17 @@ export interface ReactReduxContextValue<
noopCheck: CheckFrequency
}

let realContext: Context<ReactReduxContextValue> | null = null
const ContextKey = Symbol.for(`react-redux-context-${ReactVersion}`)
const gT = globalThis as { [ContextKey]?: Context<ReactReduxContextValue> }

function getContext() {
let realContext = gT[ContextKey]
if (!realContext) {
realContext = createContext<ReactReduxContextValue>(null as any)
if (process.env.NODE_ENV !== 'production') {
realContext.displayName = 'ReactRedux'
}
gT[ContextKey] = realContext
}
return realContext
}
Expand Down

0 comments on commit 5c76414

Please sign in to comment.