Skip to content

Instantly share code, notes, and snippets.

@thevangelist
Last active March 15, 2017 12:35
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save thevangelist/472b8e0ed020ea2c593e2e38e388eed4 to your computer and use it in GitHub Desktop.
Save thevangelist/472b8e0ed020ea2c593e2e38e388eed4 to your computer and use it in GitHub Desktop.
Redux stores initialization, dispatchers and reducer in a nutshell
const userReducer = (state={}, action) => {
switch(action.type) {
case 'CHANGE_NAME': {
// Immutable way
state = {...state, name: action.payload}
break;
}
case "CHANGE_AGE": {
state = {...state, age: action.payload}
break;
}
}
return state;
};
const defaults = {}
const tweetsReducer = (state=[], action) => {
return state;
};
// Limit changing of user to userReducer
const reducers = combineReducers({
user: userReducer,
tweets: tweetsReducer
})
const store = createStore(reducers);
store.subscribe(() => {
console.log('Store changed', store.getState())
})
// type is mandatory, payload's name can be changed
// one action can trigger multiple side effects, that are completely de-coupled from each another
store.dispatch({ type: 'CHANGE_NAME', payload: 'Will' });
store.dispatch({ type: 'CHANGE_AGE', payload: 29 });
store.dispatch({ type: 'CHANGE_AGE', payload: 23 });
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment