Skip to content

Instantly share code, notes, and snippets.

@ScottWhittaker
Last active July 6, 2016 14:57
Show Gist options
  • Save ScottWhittaker/614c7ed0ab294f89c0ff3390995d209b to your computer and use it in GitHub Desktop.
Save ScottWhittaker/614c7ed0ab294f89c0ff3390995d209b to your computer and use it in GitHub Desktop.
Redux: Store Methods
<!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>
// 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