Skip to content

Instantly share code, notes, and snippets.

@EduardoSaverin
Created February 12, 2018 15:39
Show Gist options
  • Save EduardoSaverin/00c522db9d5058b7040d552cfa5f2742 to your computer and use it in GitHub Desktop.
Save EduardoSaverin/00c522db9d5058b7040d552cfa5f2742 to your computer and use it in GitHub Desktop.
const redux = require('redux');
const createStore = redux.createStore;
const initialState = {
counter:0
}
// Order matters Reducer -> Store -> Subscription -> dispatcher
// Reducer
const reducer = (state = initialState,action) => {
if(action.type === 'INC_COUNTER'){
return {
...state,
counter: state.counter+1
}
}else if(action.type === 'ADD_COUNTER'){
return {
...state,
counter: state.counter+action.value
}
}
return state;
}
//Store
const store = new createStore(reducer);
console.log(store.getState());
//Subscription
store.subscribe(() => {
console.log('[Subscribed Data] ',store.getState());
});
// Disptacher
store.dispatch({type: 'INC_COUNTER'});
store.dispatch({type: 'ADD_COUNTER',value:10});
console.log(store.getState());
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment