connect-to-context

2.0.0 • Public • Published

connect-to-context

React: Context -> Props

Example

import connectToContext from 'connect-to-context'
 
const Test1 = connectToContext('primaryId')(TestRaw);
 
const Test2 = connectToContext(['primaryId', 'testId'])(TestRaw);
 
const Test3 = connectToContext('primaryId', (context) => ({
    identityFieldName : 'primaryId',
    experimentInputs  : {primaryId : context.primaryId}
}))(TestRaw);
 
class Provider extends React.Component {
    static childContextTypes = {
        primaryId: React.PropTypes.any,
        testId: React.PropTypes.any,
    };
 
    getChildContext() {
        return {
            primaryId: this.props.primaryId,
            testId: this.props.testId,
        }
    }
 
    render() {
        return (
            <div>
                {this.props.children}
            </div>
        );
    }
}
 
ReactDOMServer.renderToStaticMarkup(
    <Provider primaryId = {primaryId} testId = {testId}>
        <Test1/>
        <Test2/>
        <Test3/>
    </Provider>
)

Readme

Keywords

Package Sidebar

Install

npm i connect-to-context

Weekly Downloads

1

Version

2.0.0

License

MIT

Last publish

Collaborators

  • mrcheater