Skip to content

Instantly share code, notes, and snippets.

@hex13
Last active September 27, 2017 04:29
Show Gist options
  • Save hex13/3b3936cc3d74c3c0241bae924950a873 to your computer and use it in GitHub Desktop.
Save hex13/3b3936cc3d74c3c0241bae924950a873 to your computer and use it in GitHub Desktop.
redux OOP way
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