Created
November 22, 2019 12:16
-
-
Save topherPedersen/4c15c098d99d127bc17d1fb3b717a8f2 to your computer and use it in GitHub Desktop.
Redux Quickstart
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> | |
<script src='https://cdnjs.cloudflare.com/ajax/libs/redux/3.7.2/redux.min.js'></script> | |
</head> | |
<body> | |
<h1>Redux Quickstart<h1> | |
<h2>Count: <span id="counter">0</span></h2> | |
<button onclick="increment();">+</button> | |
<button onclick="decrement();">-</button> | |
<p>https://redux.js.org/introduction/getting-started</p> | |
<script> | |
function changeState(state = 0, action) { | |
switch(action.type) { | |
case 'INCREMENT': | |
return state + 1; | |
case 'DECREMENT': | |
return state - 1; | |
default: | |
return state; | |
} | |
} | |
let store = Redux.createStore(changeState); | |
store.subscribe( () => { | |
document.getElementById('counter').innerHTML = store.getState(); | |
}); | |
function increment() { | |
store.dispatch({ type: 'INCREMENT' }); | |
} | |
function decrement() { | |
store.dispatch({ type: 'DECREMENT' }); | |
} | |
</script> | |
</body> | |
</html> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment