1 | import React from "react"
|
2 | import { shallowEqual } from "./utils/utils"
|
3 | import { IValueMap } from "./types/IValueMap"
|
4 |
|
5 | export const MobXProviderContext = React.createContext<IValueMap>({})
|
6 |
|
7 | export interface ProviderProps extends IValueMap {
|
8 | children: React.ReactNode
|
9 | }
|
10 |
|
11 | export function Provider(props: ProviderProps) {
|
12 | const { children, ...stores } = props
|
13 | const parentValue = React.useContext(MobXProviderContext)
|
14 | const mutableProviderRef = React.useRef({ ...parentValue, ...stores })
|
15 | const value = mutableProviderRef.current
|
16 |
|
17 | if (__DEV__) {
|
18 | const newValue = { ...value, ...stores }
|
19 | if (!shallowEqual(value, newValue)) {
|
20 | throw new Error(
|
21 | "MobX Provider: The set of provided stores has changed. See: https://github.com/mobxjs/mobx-react#the-set-of-provided-stores-has-changed-error."
|
22 | )
|
23 | }
|
24 | }
|
25 |
|
26 | return <MobXProviderContext.Provider value={value}>{children}</MobXProviderContext.Provider>
|
27 | }
|
28 |
|
29 | Provider.displayName = "MobXProvider"
|