react-router-4-compat
Version:
A wrapped version of react router 4 which compatible with react router 3
88 lines (73 loc) • 3.89 kB
JavaScript
function _extends() { _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; }; return _extends.apply(this, arguments); }
function _objectWithoutProperties(source, excluded) { if (source == null) return {}; var target = {}; var sourceKeys = Object.keys(source); var key, i; for (i = 0; i < sourceKeys.length; i++) { key = sourceKeys[i]; if (excluded.indexOf(key) >= 0) continue; target[key] = source[key]; } if (Object.getOwnPropertySymbols) { var sourceSymbolKeys = Object.getOwnPropertySymbols(source); for (i = 0; i < sourceSymbolKeys.length; i++) { key = sourceSymbolKeys[i]; if (excluded.indexOf(key) >= 0) continue; if (!Object.prototype.propertyIsEnumerable.call(source, key)) continue; target[key] = source[key]; } } return target; }
function _inheritsLoose(subClass, superClass) { subClass.prototype.__proto__ = superClass && superClass.prototype; subClass.__proto__ = superClass; }
/* eslint-disable react/require-default-props,react/no-children-prop */
import invariant from 'invariant';
import React from 'react';
import PropTypes from 'prop-types';
import Route from 'react-router/Route';
import hoistStatics from 'hoist-non-react-statics';
var isStateless = function isStateless(Component) {
return !Component.prototype || !Component.prototype.render;
};
var withRouter4Compat = function withRouter4Compat(Component, _temp) {
var _ref = _temp === void 0 ? {} : _temp,
_ref$withRef = _ref.withRef,
withRef = _ref$withRef === void 0 ? false : _ref$withRef;
var C =
/*#__PURE__*/
function (_React$Component) {
function C() {
return _React$Component.apply(this, arguments) || this;
}
var _proto = C.prototype;
// noinspection JSUnusedGlobalSymbols
_proto.getWrappedInstance = function getWrappedInstance() {
invariant(withRef, 'To access the wrapped instance, you need to specify ' + '`{ withRef: true}` as the second argument of the withRouter() call.');
return this.wrappedInstance;
};
_proto.render = function render() {
var _this = this;
var _this$props = this.props,
wrappedComponentRef = _this$props.wrappedComponentRef,
remainingProps = _objectWithoutProperties(_this$props, ["wrappedComponentRef"]);
var wrappedComponentRefCompat = wrappedComponentRef ? function (c) {
_this.wrappedInstance = c;
return wrappedComponentRef(c);
} : function (c) {
_this.wrappedInstance = c;
};
return React.createElement(Route, {
children: function children(routeComponentProps) {
var match = routeComponentProps.match,
location = routeComponentProps.location,
history = routeComponentProps.history;
return isStateless(Component) ? React.createElement(Component, _extends({}, remainingProps, routeComponentProps, {
router: history,
routes: _this.context.routesCompat,
params: match.params,
location: location
})) : React.createElement(Component, _extends({}, remainingProps, routeComponentProps, {
router: history,
routes: _this.context.routesCompat,
params: match.params,
location: location,
ref: wrappedComponentRefCompat
}));
}
});
};
_inheritsLoose(C, _React$Component);
return C;
}(React.Component);
C.displayName = "withRouter(" + (Component.displayName || Component.name) + ")";
C.WrappedComponent = Component;
C.propTypes = {
wrappedComponentRef: PropTypes.func
};
C.contextTypes = {
routesCompat: PropTypes.array
};
return hoistStatics(C, Component);
};
export default withRouter4Compat;