Skip to content

Instantly share code, notes, and snippets.

@chrisblossom
Forked from MarcoWorms/mini-redux.js
Created November 10, 2016 15:55
Show Gist options
  • Save chrisblossom/24dfaf1bf768968327d5cc5d8fd8e70c to your computer and use it in GitHub Desktop.
Save chrisblossom/24dfaf1bf768968327d5cc5d8fd8e70c to your computer and use it in GitHub Desktop.
Redux in a nutshell
function createStore (reducers) {
var state = reducers()
const store = {
dispatch: (action) => {
state = reducers(state, action)
},
getState: () => {
return state
}
}
return store
}
const reducers = (state = { counter: 0 }, action) => {
if (!action) { return state }
switch (action.type) {
case 'INCREMENT':
return { counter: state.counter + 1 }
case 'DECREMENT':
return { counter: state.counter - 1 }
default:
return state
}
}
const store = createStore(reducers)
store.getState() // => { counter: 0 }
store.dispatch({ type: 'INCREMENT' })
store.getState() // => { counter: 1 }
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment