Skip to content

Instantly share code, notes, and snippets.

@fakenickels
Created March 20, 2018 18:32
Show Gist options
  • Select an option

  • Save fakenickels/ba8b80d26a2c18eb93bb7df20149eefe to your computer and use it in GitHub Desktop.

Select an option

Save fakenickels/ba8b80d26a2c18eb93bb7df20149eefe to your computer and use it in GitHub Desktop.
function createStore(reducer) {
let state = reducer(null, undefined);
return {
dispatch: action => { state = reducer(action, state) },
getState: () => state,
}
}
const store = createStore((action, state = 0) => {
switch(action) {
case 'INCREMENT': return state + 1
case 'DECREMENT': return state - 1
default: return state
}
})
store.getState() // => 0
store.dispatch('INCREMENT')
store.getState() // => 1
store.dispatch('DECREMENT')
store.getState() // => 0
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment