react-context-component
Version:
This is a React component that lets you add things in the context. Put simply, the [context feature](https://facebook.github.io/react/docs/context.html) basically lets you to pass some data through all nodes in the components tree.
25 lines (18 loc) • 693 B
JavaScript
import React from 'react';
import PropTypes from 'prop-types';
var withContext = function withContext() {
var contextTypes = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : {};
var contextTypeKeys = Object.keys(contextTypes);
return function (Component) {
var WithContext = function WithContext(props, context) {
var mapContextToProps = contextTypeKeys.reduce(function (acc, key) {
acc[key] = context[key];
return acc;
}, {});
return React.createElement(Component, Object.assign({}, props, mapContextToProps));
};
WithContext.contextTypes = contextTypes;
return WithContext;
};
};
export default withContext;