Last active
September 27, 2017 04:29
-
-
Save hex13/3b3936cc3d74c3c0241bae924950a873 to your computer and use it in GitHub Desktop.
redux OOP way
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 reducers = { | |
add(state, {payload: [number]}) { | |
return state + number; | |
}, | |
}; | |
function reducer(state, action) { | |
if (reducers.hasOwnProperty(action.type)) | |
return reducers[action.type](state, action); | |
return state; | |
} | |
const store = Redux.createStore(reducer, 0); | |
// ES6 proxy approach | |
// but you can achieve it without proxy | |
const actions = new Proxy(reducers, { | |
get(target, type) { | |
return (...payload) => store.dispatch(Object.assign({type}, {payload})); | |
} | |
}) | |
actions.add(10); | |
actions.add(3); | |
console.log('state', store.getState()); // 13 |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment