Skip to content

Instantly share code, notes, and snippets.

@haywoood
Created January 6, 2017 22:25
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 haywoood/12097fd200be1988a0091dfa7a7639de to your computer and use it in GitHub Desktop.
Save haywoood/12097fd200be1988a0091dfa7a7639de to your computer and use it in GitHub Desktop.
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