Created
July 12, 2016 18:52
-
-
Save takashi/2063d786a8013f8042b566a83a0e97a6 to your computer and use it in GitHub Desktop.
react-dispatcher-decorator
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 ReactDOM from "react-dom" | |
import React from 'react' | |
import {dispatcher, subscriber} from 'react-dispatcher-decorator'; | |
@dispatcher | |
class Child extends React.Component { | |
render() { | |
return <button onClick={() => this.context.dispatch('increment', 1) }>increment</button> | |
} | |
} | |
@subscriber((self, subscribe) => { | |
subscribe('increment', (prop) => { | |
self.setState({count: self.state.count + prop}) | |
}) | |
}) | |
class App extends React.Component { | |
state = { | |
count: 0 | |
} | |
render() { | |
return ( | |
<div> | |
{this.state.count} | |
<Child/> | |
</div> | |
) | |
} | |
} | |
ReactDOM.render(<App />, document.querySelector('#main')); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment