Skip to content
This repository has been archived by the owner on Dec 31, 2020. It is now read-only.

Commit

Permalink
Simplify the Provider component by using object destructuring (thanks…
Browse files Browse the repository at this point in the history
  • Loading branch information
vkrol authored and FredyC committed Aug 6, 2019
1 parent 12cce61 commit 94540de
Showing 1 changed file with 4 additions and 13 deletions.
17 changes: 4 additions & 13 deletions src/Provider.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,32 +4,23 @@ import { shallowEqual } from "./utils/utils"

export const MobXProviderContext = React.createContext({})

export function Provider(props) {
export function Provider({ children, ...stores }) {
const parentValue = React.useContext(MobXProviderContext)
const value = React.useRef({
...parentValue,
...grabStores(props)
...stores
}).current

if (process.env.NODE_ENV !== "production") {
const newValue = { ...value, ...grabStores(props) } // spread in previous state for the context based stores
const newValue = { ...value, ...stores } // spread in previous state for the context based stores
if (!shallowEqual(value, newValue)) {
throw new Error(
"MobX Provider: The set of provided stores has changed. Please avoid changing stores as the change might not propagate to all children"
)
}
}

return (
<MobXProviderContext.Provider value={value}>{props.children}</MobXProviderContext.Provider>
)
return <MobXProviderContext.Provider value={value}>{children}</MobXProviderContext.Provider>
}

Provider.displayName = "MobXProvider"

function grabStores(from) {
const res = {}
if (!from) return res
for (let key in from) if (key !== "children") res[key] = from[key]
return res
}

0 comments on commit 94540de

Please sign in to comment.