Created
March 20, 2018 18:32
-
-
Save fakenickels/ba8b80d26a2c18eb93bb7df20149eefe to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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