1
- import React , { Context , ReactNode , useMemo } from 'react'
1
+ import React , {
2
+ Context ,
3
+ ReactNode ,
4
+ unstable_createMutableSource as createMutableSource ,
5
+ useMemo ,
6
+ } from 'react'
2
7
import { ReactReduxContext , ReactReduxContextValue } from './Context'
3
- import { createSubscription } from '../utils/Subscription'
4
- import { useIsomorphicLayoutEffect } from '../utils/useIsomorphicLayoutEffect'
5
8
import type { FixTypeLater } from '../types'
6
9
import { Action , AnyAction , Store } from 'redux'
7
10
@@ -19,31 +22,18 @@ export interface ProviderProps<A extends Action = AnyAction> {
19
22
children : ReactNode
20
23
}
21
24
22
- function Provider ( { store, context, children } : ProviderProps ) {
23
- const contextValue = useMemo ( ( ) => {
24
- const subscription = createSubscription ( store )
25
- subscription . onStateChange = subscription . notifyNestedSubs
26
- return {
27
- store,
28
- subscription,
29
- }
30
- } , [ store ] )
31
-
32
- const previousState = useMemo ( ( ) => store . getState ( ) , [ store ] )
33
-
34
- useIsomorphicLayoutEffect ( ( ) => {
35
- const { subscription } = contextValue
36
- subscription . trySubscribe ( )
37
-
38
- if ( previousState !== store . getState ( ) ) {
39
- subscription . notifyNestedSubs ( )
40
- }
41
- return ( ) => {
42
- subscription . tryUnsubscribe ( )
43
- subscription . onStateChange = undefined
44
- }
45
- } , [ contextValue , previousState ] )
25
+ export function createReduxContext ( store : Store ) {
26
+ return {
27
+ storeSource : createMutableSource ( store , ( ) => store . getState ( ) ) ,
28
+ store,
29
+ }
30
+ }
46
31
32
+ function Provider ( { store, context, children } : ProviderProps ) {
33
+ const contextValue : ReactReduxContextValue = useMemo (
34
+ ( ) => createReduxContext ( store ) ,
35
+ [ store ]
36
+ )
47
37
const Context = context || ReactReduxContext
48
38
49
39
return < Context . Provider value = { contextValue } > { children } </ Context . Provider >
0 commit comments