Created
August 31, 2019 16:24
-
-
Save splincode/56661a093557a0683fb602c4ea021d78 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
<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