@vve/react-router
Version:
react router for react-helper & with keep-alive
29 lines (28 loc) • 1.17 kB
JavaScript
import _extends from "@babel/runtime/helpers/extends";
import _objectWithoutProperties from "@babel/runtime/helpers/objectWithoutProperties";
var _excluded = ["wrappedComponentRef"];
import React from 'react';
import hoistStatics from 'hoist-non-react-statics';
import invariant from 'tiny-invariant';
import RouterContext from "./router-context";
/**
* A public higher-order component to access the imperative API
*/
function withRouter(Component) {
var displayName = `withRouter(${Component.displayName || Component.name})`;
var C = props => {
var wrappedComponentRef = props.wrappedComponentRef,
remainingProps = _objectWithoutProperties(props, _excluded);
return /*#__PURE__*/React.createElement(RouterContext.Consumer, null, context => {
invariant(context, `You should not use <${displayName} /> outside a <Router>`);
return /*#__PURE__*/React.createElement(Component, _extends({}, remainingProps, context, {
ref: wrappedComponentRef
}));
});
};
C.displayName = displayName;
C.WrappedComponent = Component;
return hoistStatics(C, Component);
}
export default withRouter;
//# sourceMappingURL=with-router.js.map