UNPKG

react-router

Version:
76 lines (50 loc) 2.56 kB
'use strict'; exports.__esModule = true; var _extends = Object.assign || function (target) { for (var i = 1; i < arguments.length; i++) { var source = arguments[i]; for (var key in source) { if (Object.prototype.hasOwnProperty.call(source, key)) { target[key] = source[key]; } } } return target; }; exports.default = withRouter; var _invariant = require('invariant'); var _invariant2 = _interopRequireDefault(_invariant); var _react = require('react'); var _react2 = _interopRequireDefault(_react); var _hoistNonReactStatics = require('hoist-non-react-statics'); var _hoistNonReactStatics2 = _interopRequireDefault(_hoistNonReactStatics); var _ContextUtils = require('./ContextUtils'); var _PropTypes = require('./PropTypes'); function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; } function getDisplayName(WrappedComponent) { return WrappedComponent.displayName || WrappedComponent.name || 'Component'; } function withRouter(WrappedComponent, options) { var withRef = options && options.withRef; var WithRouter = _react2.default.createClass({ displayName: 'WithRouter', mixins: [(0, _ContextUtils.ContextSubscriber)('router')], contextTypes: { router: _PropTypes.routerShape }, propTypes: { router: _PropTypes.routerShape }, getWrappedInstance: function getWrappedInstance() { !withRef ? process.env.NODE_ENV !== 'production' ? (0, _invariant2.default)(false, 'To access the wrapped instance, you need to specify ' + '`{ withRef: true }` as the second argument of the withRouter() call.') : (0, _invariant2.default)(false) : void 0; return this.wrappedInstance; }, render: function render() { var _this = this; var router = this.props.router || this.context.router; if (!router) { return _react2.default.createElement(WrappedComponent, this.props); } var params = router.params, location = router.location, routes = router.routes; var props = _extends({}, this.props, { router: router, params: params, location: location, routes: routes }); if (withRef) { props.ref = function (c) { _this.wrappedInstance = c; }; } return _react2.default.createElement(WrappedComponent, props); } }); WithRouter.displayName = 'withRouter(' + getDisplayName(WrappedComponent) + ')'; WithRouter.WrappedComponent = WrappedComponent; return (0, _hoistNonReactStatics2.default)(WithRouter, WrappedComponent); } module.exports = exports['default'];