Skip to content

Instantly share code, notes, and snippets.

@simmo
Created December 16, 2016 12:26
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save simmo/adf5deec12fc7eaf85dc44cb1cfd1eb0 to your computer and use it in GitHub Desktop.
Save simmo/adf5deec12fc7eaf85dc44cb1cfd1eb0 to your computer and use it in GitHub Desktop.
Redux helpers
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