Skip to content

Instantly share code, notes, and snippets.

@jamiebuilds
Last active July 3, 2017 22:54
Show Gist options
  • Star 10 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save jamiebuilds/35d86c4dac146e1e84c3dbd3d37e3de1 to your computer and use it in GitHub Desktop.
Save jamiebuilds/35d86c4dac146e1e84c3dbd3d37e3de1 to your computer and use it in GitHub Desktop.
function increment(props, state) {
return {
value: state.value + props.step,
};
}
function decrement(props, state) {
return {
value: state.value - props.step,
};
}
function Counter(props: { step: number }, state = { value: 0 }, updater) {
return (
<div>
<button onClick={updater(increment)}>+</button>
<h1>{state.value}</h1>
<button onClick={updater(decrement)}>-</button>
</div>
);
}
ReactDOM.render(
<Counter step={5}/>,
document.getElementById('root')
);
let increment = (props, state) => ({
value: state.value + props.step,
})
let decrement = (props, state) => ({
value: state.value - props.step,
})
let Counter = (props: { step: number }, state = { value: 0 }, updater) => (
<div>
<button onClick={updater(increment)}>+</button>
<h1>{state.value}</h1>
<button onClick={updater(decrement)}>-</button>
</div>
)
ReactDOM.render(
<Counter step={5}/>,
document.getElementById('root')
)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment