Skip to content

Instantly share code, notes, and snippets.

@takashi
Created July 12, 2016 18:52
Show Gist options
  • Save takashi/2063d786a8013f8042b566a83a0e97a6 to your computer and use it in GitHub Desktop.
Save takashi/2063d786a8013f8042b566a83a0e97a6 to your computer and use it in GitHub Desktop.
react-dispatcher-decorator
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