Last active
December 11, 2015 11:10
-
-
Save morten-olsen/d035e167e0638f51d570 to your computer and use it in GitHub Desktop.
This file contains hidden or 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, { PropTypes, Component } from 'react'; | |
import { connect as reduxConnect } from 'react-redux'; | |
export class Split extends Component { | |
static get childContextTypes() { | |
return { | |
stateMap: PropTypes.func, | |
}; | |
} | |
getChildContext() { | |
return { | |
stateMap: this.props.stateMap, | |
}; | |
} | |
render() { | |
return (<div>{this.props.children}</div>) | |
} | |
} | |
export function connect(map) { | |
return function (WrappedComponent) { | |
return class ConnectSplit extends Component { | |
static get contextTypes() { | |
return { | |
stateMap: PropTypes.func, | |
} | |
} | |
render() { | |
const newMap = this.context.stateMap ? (state) => { | |
const newState = this.context.stateMap(state); | |
const resultState = map(newState); | |
return resultState; | |
} : map; | |
const NewComponent = reduxConnect(newMap)(WrappedComponent); | |
return (<NewComponent />); | |
} | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment