Skip to content

Instantly share code, notes, and snippets.

@morten-olsen
Last active December 11, 2015 11:10
Show Gist options
  • Save morten-olsen/d035e167e0638f51d570 to your computer and use it in GitHub Desktop.
Save morten-olsen/d035e167e0638f51d570 to your computer and use it in GitHub Desktop.
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