simple redux store example
HTML
<html>
<head>
<script src="https://cdnjs.cloudflare.com/ajax/libs/redux/4.0.1/redux.js">
</script>
</head>
<body>
</body>
</html>
JavaScript
// register our reducer
const counter = ( state = 0, action ) => {
switch(action.type) {
case 'INCREMENT':
return state + 1
case 'DECREMENT':
return state - 1
default:
return state
}
}
// create a store by passing it a reducer.
const { createStore } = Redux
const store = createStore(counter)
// the subscribe method tells the store to fire a callback when any action is dispatched.
store.subscribe( () => {
console.log('fired!')
})
console.log(store.getState())
// dispatch an action with a type
store.dispatch({type:'INCREMENT'})
console.log(store.getState())
store.dispatch({type:'DECREMENT'})
console.log(store.getState())
store.dispatch({type:'INCREMENT'})
console.log(store.getState())