-
-
Save cef62/e094a0691af3e95a323bd52185d475a3 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 from 'react' | |
const provideContext = (contextKey, contextType) => ( | |
React.createClass({ | |
childContextTypes: { | |
[contextKey]: contextType | |
}, | |
getChildContext() { | |
const { children, ...props } = this.props | |
return { | |
[contextKey]: props | |
} | |
}, | |
render() { | |
return React.Children.only(this.props.children) | |
} | |
}) | |
) | |
export default provideContext |
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 from 'react' | |
export default (contextKey, contextType) => ( | |
(Component) => ( | |
React.createClass({ | |
contextTypes: { | |
[contextKey]: contextType | |
}, | |
render() { | |
const props = { | |
...this.props, | |
[contextKey]: this.context[contextKey] | |
} | |
return <Component {...props}/> | |
} | |
}) | |
) | |
) | |
// usage | |
const Something = withContext('router', PropTypes.object)(React.createClass({ | |
render() { | |
this.props.router | |
} | |
})) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment