Last active
September 18, 2018 04:07
-
-
Save filipelinhares/1075263fbc63ec7a44fa3107e9c352fa to your computer and use it in GitHub Desktop.
Redux
This file contains 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
const counter = (state = 0, action) => { | |
switch (action.type) { | |
case 'INCREMENT': | |
return state + 1; | |
case 'DECREMENT': | |
return state - 1; | |
default: | |
return state; | |
} | |
} | |
const store = createStore(counter); | |
store.getState(); | |
store.dispatch({ type: 'INCREMENT' }); | |
store.subscribe(() => console.log) |
This file contains 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
const createStore = (reducer) => { | |
let state; | |
let listeners = []; | |
const getState = () => state; | |
const dispatch = (action) => { | |
state = reducer(state, action); | |
listeners.forEach(listener => listener()); | |
}; | |
const subscribe = (listener) => { | |
listeners.push(listener); | |
return () => { | |
listeners.filter((l) => l !== listener); | |
}; | |
}; | |
dispatch({}); | |
return { getState, dispatch, subscribe }; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment