Created
November 25, 2018 23:20
-
-
Save tiagolpadua/06aed7567716f0ece43ca9e4457643b9 to your computer and use it in GitHub Desktop.
This file contains 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
<!DOCTYPE html> | |
<html> | |
<head> | |
<title>Um contador</title> | |
<script src="https://unpkg.com/redux@latest/dist/redux.min.js"></script> | |
</head> | |
<body> | |
<div> | |
<p> | |
Clicou: | |
<span id="value">0</span> vezes | |
<button id="increment">+</button> | |
<button id="decrement">-</button> | |
<button id="incrementIfOdd">Incrementa se for ímpar</button> | |
<button id="incrementAsync">Incrementa assíncrono</button> | |
</p> | |
</div> | |
<script> | |
var valueEl = document.getElementById('value'); | |
function counter(state, action) { | |
if (typeof state === 'undefined') { | |
return 0 | |
} | |
switch (action.type) { | |
case 'INCREMENT': | |
return state + 1 | |
case 'DECREMENT': | |
return state - 1 | |
default: | |
return state | |
} | |
} | |
var valueEl = document.getElementById('value'); | |
function render() { | |
valueEl.innerHTML = store.getState().toString() | |
} | |
var store = Redux.createStore(counter) | |
render() | |
store.subscribe(render) | |
document.getElementById('increment') | |
.addEventListener('click', function () { | |
store.dispatch({ type: 'INCREMENT' }) | |
}); | |
document.getElementById('decrement') | |
.addEventListener('click', function () { | |
store.dispatch({ type: 'DECREMENT' }) | |
}); | |
document.getElementById('incrementIfOdd') | |
.addEventListener('click', function () { | |
if (store.getState() % 2 !== 0) { | |
store.dispatch({ type: 'INCREMENT' }) | |
} | |
}); | |
document.getElementById('incrementAsync') | |
.addEventListener('click', function () { | |
setTimeout(function () { | |
store.dispatch({ type: 'INCREMENT' }) | |
}, 1000) | |
}); | |
</script> | |
</body> | |
</html> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment