Created
December 16, 2016 12:26
-
-
Save simmo/adf5deec12fc7eaf85dc44cb1cfd1eb0 to your computer and use it in GitHub Desktop.
Redux helpers
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
export const mapDispatchToProps = actions => dispatch => Object.keys(actions).reduce((obj, key) => { | |
obj.actions[key] = bindActionCreators(actions[key], dispatch) | |
return obj | |
}, { actions: {} }) | |
// connect(null, mapDispatchToProps({ action1, action2 }))(ReactComponent) | |
// => this.props.actions.action1, this.props.actions.action2 | |
export const mapStateToProps = properties => store => properties.reduce((obj, property) => { | |
if (store.hasOwnProperty(property)) { | |
obj[property] = store[property] | |
} else { | |
console && console.error(`Cannot read '${property}' in store`) | |
} | |
return obj | |
}, {}) | |
// connect(mapStateToProps(['subStore']), null)(ReactComponent) | |
// => this.props.subStore | |
export function createReducer(initialState = [], handlers = {}) { | |
return function reducer(state = initialState, action) { | |
if (handlers.hasOwnProperty(action.type)) { | |
return handlers[action.type](state, action) | |
} else { | |
return state | |
} | |
} | |
} | |
// createReducer({ something: false }, { | |
// ['MAKE_SOMETHING_TRUE'](state) { | |
// return { | |
// ...state, | |
// something: true | |
// } | |
// } | |
// }) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment