Skip to content

Instantly share code, notes, and snippets.

@splincode
Created August 31, 2019 16:24
Show Gist options
  • Save splincode/56661a093557a0683fb602c4ea021d78 to your computer and use it in GitHub Desktop.
Save splincode/56661a093557a0683fb602c4ea021d78 to your computer and use it in GitHub Desktop.
<script src="https://unpkg.com/redux@latest/dist/redux.min.js"></script>
<p>
Counter: <span id="value">0</span>
<button id="increment">+</button>
<button id="decrement">-</button>
</p>
<script>
function counterReducer(state, action) {
if (typeof state === 'undefined') {
return 0;
}
switch (action.type) {
case 'INCREMENT':
return state + 1
case 'DECREMENT':
return state - 1
default:
return state
}
}
const store = Redux.createStore(counterReducer)
store.subscribe(() => {
const valueElement = document.getElementById('value');
valueElement.innerHTML = store.getState().toString()
})
document.getElementById('increment')
.addEventListener('click', function () {
store.dispatch({ type: 'INCREMENT' })
})
document.getElementById('decrement')
.addEventListener('click', function () {
store.dispatch({ type: 'DECREMENT' })
})
</script>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment