Skip to content

Instantly share code, notes, and snippets.

@antoniojps
Created December 11, 2018 18:30
Show Gist options
  • Select an option

  • Save antoniojps/15eac93f7c11ece2febf1ad8e1274a39 to your computer and use it in GitHub Desktop.

Select an option

Save antoniojps/15eac93f7c11ece2febf1ad8e1274a39 to your computer and use it in GitHub Desktop.
// default state
const defaultState = {
welcome: 'Hello',
otherState: 'some stuff',
}
// reducer, always returns new object based on the action type
const greeting = (state = defaultState, action) => {
switch (action.type) {
case 'GREET_ME':
return {
...state,
welcome: 'Hello Antonio',
}
case 'GREET_WORLD':
return {
...state,
welcome: 'Hello World',
}
default:
return state
}
}
// creates store from reducers
const store = createStore(greeting)
console.log(store.getState())
// {welcome: "Hello", otherState: "some stuff"}
// action is an object describing what we are dispatching
store.dispatch({
type: 'GREET_ME'
})
console.log(store.getState())
// {welcome: "Hello Antonio", otherState: "some stuff"}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment