react-multiple-context-hoc
TypeScript icon, indicating that this package has built-in type declarations

0.1.0 • Public • Published

react-multiple-context-hoc

npm version

Library to get multiple values from react context via HOC.

You can easy get any number of values from react context inside functional components using useContext, but classes can get only one value. To solve this problem you can use this HOC.

import React from 'react';
import withContext from 'react-multiple-context-hoc';
 
class MyComponent extends React.Component<{
    stringPropName: string;
    numberPropName: number;
}> {
    render() {
        return (
            <span>
                {this.props.stringPropName} {this.props.numberPropName}
            </span>
        );
    }
}
 
const stringContext = React.createContext<string>('');
const numberContext = React.createContext<number>(0);
 
const WrappedComponent = withContext({
    stringPropName: stringContext,
    numberPropName: numberContext,
})(MyComponent);

Dependencies (0)

    Dev Dependencies (18)

    Package Sidebar

    Install

    npm i react-multiple-context-hoc

    Weekly Downloads

    2

    Version

    0.1.0

    License

    MIT

    Unpacked Size

    8.65 kB

    Total Files

    7

    Last publish

    Collaborators

    • megazazik