Created
January 6, 2017 22:25
-
-
Save haywoood/12097fd200be1988a0091dfa7a7639de 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
export const reusableConnect = (_localMapStateToProps, _localActions, globalActions = DEFAULT_MAP) => Component => { | |
const localMapStateToProps = _localMapStateToProps || defaultMapStateToProps | |
const localActions = _localActions || {} | |
const _mapStateToProps = getInstanceState => (state, props) => { | |
const localState = getInstanceState(state) | |
const retVal = localMapStateToProps(localState, state, props) | |
if (typeof(retVal) === "function") { | |
return localMapStateToProps(getInstanceState) | |
} else { | |
return retVal | |
} | |
} | |
return React.createClass({ | |
displayName: `Reusable Connect(${Component.displayName || Component.name})`, | |
getInitialState() { | |
return { | |
Component: null | |
} | |
}, | |
contextTypes: { | |
_reusable: ReusableShape | |
}, | |
componentWillMount() { | |
const {namespace, passedProps, getInstanceState, componentName} = this.context._reusable | |
const _localActions = bindLocalActions(localActions, namespace, componentName, passedProps) | |
const actions = merge(_localActions, globalActions) | |
const Connected = connect(_mapStateToProps(getInstanceState), actions)(Component) | |
this.setState({Component: Connected}) | |
}, | |
render() { | |
const {Component} = this.state | |
return Component ? <Component {...this.props} /> : <div></div> | |
} | |
}) | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment