Created
May 31, 2017 00:23
-
-
Save scottdomes/377a504c51d997df0f87f4703498b343 to your computer and use it in GitHub Desktop.
This file contains hidden or 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 { Provider } from 'mobx-react' | |
| // Importing our own stores | |
| import { UserStore, ContactStore } from '../stores' | |
| const state = { | |
| userStore: UserStore, | |
| contactStore: ContactStore | |
| } | |
| export default function App() { | |
| return ( | |
| <Provider state={state}> | |
| <ContactList /> | |
| </Provider> | |
| ) | |
| } |
This file contains hidden or 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 { observer } from 'mobx-react' | |
| // Importing our custom connect function from above | |
| import connect from '../mobx-connect' | |
| @observer | |
| class ContactList extends React.Component { | |
| render() { | |
| return ( | |
| <div> | |
| <p>Welcome, { this.props.user.name }</p> | |
| <p>You have { this.props.contacts.length } contacts</p> | |
| </div> | |
| ) | |
| } | |
| } | |
| const mapStateToProps = (state => { | |
| return ({ | |
| user: state.userStore.user, | |
| contacts: state.contactStore.contacts | |
| }) | |
| }) | |
| export default connect(mapStateToProps)(ContactList) | |
This file contains hidden or 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 { inject } from 'mobx-react' | |
| const connect = (mapStateToProps) => { | |
| return (component) => { | |
| return inject((stores, props, context) => { return mapStateToProps(context.mobxStores.state) })(component) | |
| } | |
| } | |
| export default connect |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment