Last active
August 16, 2018 06:13
-
-
Save sakulstra/56bf4a361417eebe089e7380e1e8e820 to your computer and use it in GitHub Desktop.
next.js with mobx
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
| const messages = [ | |
| "nice to have you here", | |
| "i like you", | |
| "welcome <3", | |
| "let's drink a beer together mate!", | |
| ]; | |
| let store = null; | |
| class Store { | |
| @observable helloMessage = ''; | |
| constructor() { | |
| this.helloMessage = messages[Math.floor(Math.random() * (messages.length - 1))]; | |
| } | |
| @action start = () => { | |
| this.timer = setInterval(() => { | |
| this.helloMessage = messages[Math.floor(Math.random() * (messages.length - 1))]; | |
| }, 10000); | |
| } | |
| stop = () => clearInterval(this.timer) | |
| } | |
| export default function initStore() { | |
| return new Store(); | |
| } |
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
| export default class HomePage extends Component { | |
| constructor (props) { | |
| super(props); | |
| this.store = initBaseStore(); | |
| } | |
| render(){ | |
| return ( | |
| <Provider BaseStore={this.store}> | |
| <FriendlyHello /> | |
| </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
| @inject('BaseStore') @observer | |
| export default class FriendlyHello extends Component { | |
| componentDidMount () { | |
| this.props.BaseStore.start() | |
| } | |
| componentWillUnmount () { | |
| this.props.BaseStore.stop() | |
| } | |
| render () { | |
| return ( | |
| <div> | |
| {this.props.BaseStore.helloMessage} | |
| </div> | |
| ) | |
| } | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment