Last active
July 6, 2016 14:57
-
-
Save ScottWhittaker/614c7ed0ab294f89c0ff3390995d209b to your computer and use it in GitHub Desktop.
Redux: Store Methods
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> | |
<meta charset="utf-8"> | |
<title>Getting Started with Redux</title> | |
</head> | |
<body> | |
<script src="https://cdnjs.cloudflare.com/ajax/libs/redux/3.3.1/redux.js"></script> | |
<script src="script.js"></script> | |
</body> | |
</html> |
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
// https://egghead.io/lessons/javascript-redux-store-methods-getstate-dispatch-and-subscribe | |
// Reducer for the counter example | |
const counter = (state = 0, action) => { | |
switch(action.type) { | |
case 'INCREMENT': | |
return state + 1; | |
case 'DECREMENT': | |
return state - 1; | |
default: | |
return state; | |
} | |
} | |
const { createStore } = Redux; | |
const store = createStore(counter); | |
const render = function() { | |
document.body.innerText = store.getState(); | |
} | |
// register a callback that the redux store will call | |
// any time an action has been dispatched | |
store.subscribe(render); | |
render(); | |
document.addEventListener('click', function() { | |
store.dispatch({ type: 'INCREMENT' }); | |
}); | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment