Last active
February 12, 2020 04:19
-
-
Save juddey/e0e2d9cbdb1dc3a181901f5d4be2e052 to your computer and use it in GitHub Desktop.
Mobx
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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