Skip to content

Instantly share code, notes, and snippets.

@loveyunk
Created February 14, 2019 13:54
Show Gist options
  • Save loveyunk/58a42f0f596b72764ed41e9d34449362 to your computer and use it in GitHub Desktop.
Save loveyunk/58a42f0f596b72764ed41e9d34449362 to your computer and use it in GitHub Desktop.
const { createStore } = require('redux');
const initialState = {
age: 21
};
const myReducer = (state = initialState, action) => {
let newState = { ...state };
if (action.type === 'ADD') {
newState.age += 1;
}
if (action.type === 'SUBSTRACT') {
newState.age -= 1;
}
return newState;
};
const store = createStore(myReducer);
store.subscribe(() => {
console.log('store change: ' + JSON.stringify(store.getState()));
});
store.dispatch({ type: 'ADD' });
store.dispatch({ type: 'ADD' });
store.dispatch({ type: 'ADD' });
store.dispatch({ type: 'SUBSTRACT' });
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment