0.1.0 • Published 4 years ago

react-multiple-context-hoc v0.1.0

Weekly downloads
2
License
MIT
Repository
github
Last release
4 years ago

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);