Skip to content

Instantly share code, notes, and snippets.

@GregoryCollett
Created March 14, 2017 10:47
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 GregoryCollett/952992697e4737818b9159b82aa5ae52 to your computer and use it in GitHub Desktop.
Save GregoryCollett/952992697e4737818b9159b82aa5ae52 to your computer and use it in GitHub Desktop.
import React, { Component } from 'react';
export const withReducer = (
stateName = 'internalState',
dispatchName = 'internalDispatch',
reducer = (state) => state,
initialState,
) => (BaseComponent) => class WithReducer extends Component {
state = {
value: initialState,
};
dispatch = action => this.setState({
value: reducer(this.state.value, action),
});
render() {
const mappedProps = {
[stateName]: this.state.value,
[dispatchName]: this.dispatch,
};
return (
<BaseComponent
{...this.props}
{...mappedProps}
/>
);
}
};
export default withReducer;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment