Last active
January 22, 2017 11:06
-
-
Save axross/e030f56753573224d83aeef11378ccdc 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
const Counter = ({ count, onIncrementClick, onDecrementClick, onIncrementAsyncClick }) => ( | |
<div> | |
{count} | |
: | |
<button onClick={onIncrementClick}>+</button> | |
/ | |
<button onClick={onDecrementClick}>-</button> | |
/ | |
<br/> | |
<button onClick={onIncrementAsyncClick}>+ Async</button> | |
</div> | |
); | |
export default Counter; |
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
class CounterController extends React.Component<> { | |
constructor() { | |
this.onIncrementClick = this.onIncrementClick.bind(this); | |
this.onDecrementClick = this.onDecrementClick.bind(this); | |
this.onIncrementAsyncClick = this.onIncrementAsyncClick.bind(this); | |
} | |
onIncrementClick() { | |
// 規模そこそこ以上だったら、直接dispatchをpropsで受け取ってゴニョゴニョじゃなくて、 | |
// 腹にdispatchを持ったFooServiceのインスタンスをcontextかpropsかで受け取って、this.props.fooService.bar()みたいにすると思う | |
this.props.dispatch({ type: "INCREMENT" }); | |
} | |
onDecrementClick() { | |
this.props.dispatch({ type: "DECREMENT" }); | |
} | |
onIncrementAsyncClick() { | |
setTimeout(() => this.props.dispatch({ type: "INCREMENT" }), 100); | |
} | |
render() { | |
return ( | |
<Counter | |
onIncrementClick={this.onIncrementClick} | |
onDecrementClick={this.onDecrementClick} | |
onIncrementAsyncClick={this.onIncrementAsyncClick} | |
/> | |
); | |
} | |
} | |
export default CounterController; |
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
document.addEventListener('DOMContentLoaded' () => { | |
// ... | |
const ConnectedCounterController = connect(({ counter }) => ({ counter }))(CounterController); | |
// ... | |
}); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment