Skip to content

Instantly share code, notes, and snippets.

@tmcw
Created July 29, 2016 01:34
Show Gist options
  • Save tmcw/f103f82ebf79ac4474a943d29a1f2f1f to your computer and use it in GitHub Desktop.
Save tmcw/f103f82ebf79ac4474a943d29a1f2f1f to your computer and use it in GitHub Desktop.
<html>
<script src="https://code.jquery.com/jquery-3.1.0.js" integrity="sha256-slogkvB1K3VOkzAI8QITxV3VzpOnkeNVsKvtkYLMjfk=" crossorigin="anonymous"></script>
<h1 class='js-target-count'></h1>
<button data-action='{"type":"increment"}'>+</button>
<button data-action='{"type":"decrement"}'>-</button>
<script>
var state = {
count: 0
};
function reducer(state, action) {
switch (action.type) {
case 'increment': state.count++; break;
case 'decrement': state.count--; break;
}
return state;
}
$('[data-action]').click(function(e) {
e.preventDefault();
render(state = reducer(state, $(this).data('action')));
});
render(state);
function render(state) {
$('.js-target-count').text(state.count);
}
</script>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment