const INCREMENT = 'INCREMENT';
const DECREMENT = 'DECREMENT';
function increment() {
return {type: INCREMENT};
}
function decrement() {
return {type: DECREMENT};
}
// Reducer
function counterReducer(state = {count: 0}, action) {
switch (action.type) {
case INCREMENT:
return {count: state.count + 1};
case DECREMENT:
return {count: state.count - 1};
default:
return state;
}
}
// Store
const store = Redux.createStore(counterReducer);
// View
const countElement = document.getElementById('count');
const incrementButton = document.getElementById('increment');
const decrementButton = document.getElementById('decrement');
function render() {
countElement.textContent = store.getState().count;
}
store.subscribe(render);
incrementButton.addEventListener('click', () => {
store.dispatch(increment());
});
decrementButton.addEventListener('click', () => {
store.dispatch(decrement());
});
render();