Created
March 14, 2017 10:47
-
-
Save GregoryCollett/952992697e4737818b9159b82aa5ae52 to your computer and use it in GitHub Desktop.
This file contains 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
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