Skip to content

Instantly share code, notes, and snippets.

@juddey
Last active February 12, 2020 04:19
Show Gist options
  • Save juddey/e0e2d9cbdb1dc3a181901f5d4be2e052 to your computer and use it in GitHub Desktop.
Save juddey/e0e2d9cbdb1dc3a181901f5d4be2e052 to your computer and use it in GitHub Desktop.
Mobx
import React from 'react'
import { types } from 'mobx-state-tree'
const Person = types
.model("person", {
name: types.optional(types.string, '')
})
.actions(self => ({
setName (name) {
self.name = name
}
}))
const RootStore = types.model("RootStore", {
person: types.map(Person)
})
const store = RootStore.create({
person: {}
})
const storeContext = React.createContext(store)
const withStoreProvider = C => props => {
return (
<storeContext.Provider value={store}>
<C {...props} />
</storeContext.Provider>
)
}
const useStore = () => React.useContext(storeContext)
export { withStoreProvider, useStore }
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment