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.
36 lines (23 loc) • 957 B
JavaScript
;
exports.__esModule = true;
var _react = require('react');
var _react2 = _interopRequireDefault(_react);
var _propTypes = require('prop-types');
var _propTypes2 = _interopRequireDefault(_propTypes);
function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
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 _react2.default.createElement(Component, Object.assign({}, props, mapContextToProps));
};
WithContext.contextTypes = contextTypes;
return WithContext;
};
};
exports.default = withContext;