UNPKG

@vve/react-router

Version:

react router for react-helper & with keep-alive

58 lines (56 loc) 2.03 kB
import _objectSpread from "@babel/runtime/helpers/objectSpread2"; import React from 'react'; import PropTypes from 'prop-types'; import { createLocation, locationsAreEqual } from 'history'; import invariant from 'tiny-invariant'; import Lifecycle from "./lifecycle"; import RouterContext from "./router-context"; import generatePath from "./generate-path"; /** * The public API for navigating programmatically with a component. */ function Redirect(_ref) { var computedMatch = _ref.computedMatch, to = _ref.to, _ref$push = _ref.push, push = _ref$push === void 0 ? false : _ref$push; return /*#__PURE__*/React.createElement(RouterContext.Consumer, null, context => { invariant(context, 'You should not use <Redirect> outside a <Router>'); var history = context.history, staticContext = context.staticContext; var method = push ? history.push : history.replace; var location = createLocation(computedMatch ? typeof to === 'string' ? generatePath(to, computedMatch.params) : _objectSpread(_objectSpread({}, to), {}, { pathname: generatePath(to.pathname, computedMatch.params) }) : to); // When rendering in a static context, // set the new location immediately. if (staticContext) { method(location); return null; } return /*#__PURE__*/React.createElement(Lifecycle, { onMount: () => { method(location); }, onUpdate: (self, prevProps) => { var prevLocation = createLocation(prevProps.to); if (!locationsAreEqual(prevLocation, _objectSpread(_objectSpread({}, location), {}, { key: prevLocation.key }))) { method(location); } }, to: to }); }); } if (process.env.NODE_ENV !== 'production') { Redirect.propTypes = { push: PropTypes.bool, from: PropTypes.string, computedMatch: PropTypes.string, to: PropTypes.oneOfType([PropTypes.string, PropTypes.object]).isRequired }; } export default Redirect; //# sourceMappingURL=redirect.js.map