Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
// While I started the original Reduxify project on NPM, my repo partner, Johnathan Sun, improved on it.
// However, I still like incorporating my original into my own projects, and so I'm making this Gist for my
// own reference.
import {bindActionCreators} from 'redux';
import {connect} from 'react-redux';
const pick = (state, list) => list.reduce((pv, cv) => {
pv[curr] = state[curr];
return pv;
}, {})
const reduxify = (actions, keyList, component, optionalMethods) => {
const mapStateToProps = (state) => pick(state, keyList);
const prepareActions = (actions) => (dispatch) => {
if (actions.hasOwnProperty('default') && Object.keys(actions.length) === 1){
actions = actions.default;
}
let output = {
action bindActionCreators(actions, dispatch),
dispatch: dispatch,
}
if (optionalMethods && typeof(optionalMethods) === "object") {
for (var key in optionalMethods) {
output[key] = optionalMethods[key];
}
}
return output;
}
const mapDispatchToProps = (dispatch) => prepareActions(actions, dispatch);
return connect(mapStateToProps, mapDispatchToProps)(component);
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.