1
1
import { createContext } from 'react'
2
+ import type { Context } from 'react'
2
3
import type { Action , AnyAction , Store } from 'redux'
3
4
import type { Subscription } from '../utils/Subscription'
4
5
import { StabilityCheck } from '../hooks/useSelector'
@@ -13,13 +14,31 @@ export interface ReactReduxContextValue<
13
14
stabilityCheck : StabilityCheck
14
15
}
15
16
16
- export const ReactReduxContext =
17
- /*#__PURE__*/ createContext < ReactReduxContextValue > ( null as any )
17
+ let realContext : Context < ReactReduxContextValue > | null = null
18
+ function getContext ( ) {
19
+ if ( ! realContext ) {
20
+ realContext = createContext < ReactReduxContextValue > ( null as any )
21
+ if ( process . env . NODE_ENV !== 'production' ) {
22
+ realContext . displayName = 'ReactRedux'
23
+ }
24
+ }
25
+ return realContext
26
+ }
18
27
19
- export type ReactReduxContextInstance = typeof ReactReduxContext
28
+ export const ReactReduxContext = /*#__PURE__*/ new Proxy (
29
+ { } as Context < ReactReduxContextValue > ,
30
+ /*#__PURE__*/ new Proxy < ProxyHandler < Context < ReactReduxContextValue > > > (
31
+ { } ,
32
+ {
33
+ get ( _ , handler ) {
34
+ const target = getContext ( )
35
+ // @ts -ignore
36
+ return ( _target , ...args ) => Reflect [ handler ] ( target , ...args )
37
+ } ,
38
+ }
39
+ )
40
+ )
20
41
21
- if ( process . env . NODE_ENV !== 'production' ) {
22
- ReactReduxContext . displayName = 'ReactRedux'
23
- }
42
+ export type ReactReduxContextInstance = typeof ReactReduxContext
24
43
25
44
export default ReactReduxContext
0 commit comments