Skip to content

Instantly share code, notes, and snippets.

@axross
Last active January 22, 2017 11:06
Show Gist options
  • Save axross/e030f56753573224d83aeef11378ccdc to your computer and use it in GitHub Desktop.
Save axross/e030f56753573224d83aeef11378ccdc to your computer and use it in GitHub Desktop.
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;
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;
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