Last active
March 4, 2018 17:00
-
-
Save geoffmiller/7fa30eb060378e0c626431a8ef1f3b1e to your computer and use it in GitHub Desktop.
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
//// MyComponent/MyComponent.js | |
const MyComponent = ({class, text, setText}) => ( | |
<div | |
className={class} | |
onClick={setText} | |
>{text}</div> | |
) | |
export default MyComponent | |
//// | |
//// stores/MyStore.store.js | |
import { observable } from 'mobx' | |
const MyStore = observable({ | |
class: '', | |
text: '' | |
}) | |
export default MyStore | |
//// | |
//// stores/MyStore.actions.js | |
import { action } from 'mobx'; | |
const MyStoreActions = store => { | |
const setClass = action((value) => store.class = value) | |
const setText = action((value) => store.text = value) | |
return { | |
setClass, | |
setText | |
} | |
} | |
export default MyStoreActions; | |
//// | |
// MyComponent/index.js | |
import { inject, observer } from 'mobx-react'; | |
import { compose, withProps, withHandlers } from 'recompose'; | |
import MyComponent from './MyComponent' | |
import MyStoreActions from './MyStore.actions' | |
const enhance = compose( | |
inject('MyStore'), | |
observer, | |
withProps(({MyStore}) => { | |
class: MyStore.class, | |
text: MyStore.text, | |
actions: MyStoreActions(MyStore) | |
}), | |
withHandlers({ | |
setClass: ({actions}) => (classToUpdate) => { actions.setClass(classToUpdate) }, | |
setText: ({actions}) => (textToUpdate) => { actions.setText(textToUpdate) }, | |
}), | |
) | |
export default MyComponent | |
//// | |
//// App.js | |
import { observer, Provider } from 'mobx-react'; | |
import myStore from '../stores/MyStore' | |
const stores = { | |
myStore, | |
... | |
}; | |
const App = () => { | |
return ( | |
<Provider {...stores}> | |
<MyComponent /> | |
... | |
</Provider> | |
) | |
} | |
export default observer(App); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment