A Pen by Etienne Deladonchamps on CodePen.
Created
September 10, 2017 09:38
-
-
Save etienne-dldc/7018042ce4512ff0c3ace31dec772527 to your computer and use it in GitHub Desktop.
17 - Redux
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
<div id="logger" style="position: fixed; left: 10px; top: 0px"></div> |
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
// Logger: not part of the demo | |
var loggerEl = document.getElementById('logger'); | |
function log(text) { | |
const line = document.createElement("span"); | |
line.innerText = text; | |
loggerEl.appendChild(line); | |
loggerEl.appendChild(document.createElement("br")); | |
} | |
/////// | |
// reducer | |
function counterReducer(state = 0, action) { | |
switch (action.type) { | |
case 'INCREMENT': | |
return state + 1 | |
case 'DECREMENT': | |
return state - 1 | |
default: | |
return state | |
} | |
} | |
// create the store | |
let store = Redux.createStore(counterReducer) | |
// subscribe | |
store.subscribe(() => | |
log('state : ' + store.getState()) | |
) | |
// We can then change the state by dispatching actions | |
store.dispatch({ type: 'INCREMENT' }) | |
store.dispatch({ type: 'INCREMENT' }) | |
store.dispatch({ type: 'DECREMENT' }) |
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
<script src="https://cdnjs.cloudflare.com/ajax/libs/redux/3.7.2/redux.min.js"></script> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment