source: node_modules/react-redux/src/components/Provider.tsx

Last change on this file was a762898, checked in by istevanoska <ilinastevanoska@…>, 5 months ago

Added visualizations

  • Property mode set to 100644
File size: 3.3 KB
Line 
1import type { Context, ReactNode } from 'react'
2import { React } from '../utils/react'
3import type { Action, Store, UnknownAction } from 'redux'
4import type { DevModeCheckFrequency } from '../hooks/useSelector'
5import { createSubscription } from '../utils/Subscription'
6import { useIsomorphicLayoutEffect } from '../utils/useIsomorphicLayoutEffect'
7import type { ReactReduxContextValue } from './Context'
8import { ReactReduxContext } from './Context'
9
10export interface ProviderProps<
11 A extends Action<string> = UnknownAction,
12 S = unknown,
13> {
14 /**
15 * The single Redux store in your application.
16 */
17 store: Store<S, A>
18
19 /**
20 * An optional server state snapshot. Will be used during initial hydration render if available, to ensure that the UI output is consistent with the HTML generated on the server.
21 */
22 serverState?: S
23
24 /**
25 * Optional context to be used internally in react-redux. Use React.createContext() to create a context to be used.
26 * If this is used, you'll need to customize `connect` by supplying the same context provided to the Provider.
27 * Set the initial value to null, and the hooks will error
28 * if this is not overwritten by Provider.
29 */
30 context?: Context<ReactReduxContextValue<S, A> | null>
31
32 /**
33 * Determines the frequency of stability checks for all selectors.
34 * This setting overrides the global configuration for
35 * the `useSelector` stability check, allowing you to specify how often
36 * these checks should occur in development mode.
37 *
38 * @since 8.1.0
39 */
40 stabilityCheck?: DevModeCheckFrequency
41
42 /**
43 * Determines the frequency of identity function checks for all selectors.
44 * This setting overrides the global configuration for
45 * the `useSelector` identity function check, allowing you to specify how often
46 * these checks should occur in development mode.
47 *
48 * **Note**: Previously referred to as `noopCheck`.
49 *
50 * @since 9.0.0
51 */
52 identityFunctionCheck?: DevModeCheckFrequency
53
54 children: ReactNode
55}
56
57function Provider<A extends Action<string> = UnknownAction, S = unknown>(
58 providerProps: ProviderProps<A, S>,
59) {
60 const { children, context, serverState, store } = providerProps
61
62 const contextValue = React.useMemo(() => {
63 const subscription = createSubscription(store)
64
65 const baseContextValue = {
66 store,
67 subscription,
68 getServerState: serverState ? () => serverState : undefined,
69 }
70
71 if (process.env.NODE_ENV === 'production') {
72 return baseContextValue
73 } else {
74 const { identityFunctionCheck = 'once', stabilityCheck = 'once' } =
75 providerProps
76
77 return /* @__PURE__ */ Object.assign(baseContextValue, {
78 stabilityCheck,
79 identityFunctionCheck,
80 })
81 }
82 }, [store, serverState])
83
84 const previousState = React.useMemo(() => store.getState(), [store])
85
86 useIsomorphicLayoutEffect(() => {
87 const { subscription } = contextValue
88 subscription.onStateChange = subscription.notifyNestedSubs
89 subscription.trySubscribe()
90
91 if (previousState !== store.getState()) {
92 subscription.notifyNestedSubs()
93 }
94 return () => {
95 subscription.tryUnsubscribe()
96 subscription.onStateChange = undefined
97 }
98 }, [contextValue, previousState])
99
100 const Context = context || ReactReduxContext
101
102 return <Context.Provider value={contextValue}>{children}</Context.Provider>
103}
104
105export default Provider
Note: See TracBrowser for help on using the repository browser.