react-router.js 25 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750
  1. import _inheritsLoose from '@babel/runtime/helpers/esm/inheritsLoose';
  2. import React from 'react';
  3. import PropTypes from 'prop-types';
  4. import { createMemoryHistory, createLocation, locationsAreEqual, createPath } from 'history';
  5. import warning from 'tiny-warning';
  6. import createContext from 'mini-create-react-context';
  7. import invariant from 'tiny-invariant';
  8. import _extends from '@babel/runtime/helpers/esm/extends';
  9. import pathToRegexp from 'path-to-regexp';
  10. import { isValidElementType } from 'react-is';
  11. import _objectWithoutPropertiesLoose from '@babel/runtime/helpers/esm/objectWithoutPropertiesLoose';
  12. import hoistStatics from 'hoist-non-react-statics';
  13. // TODO: Replace with React.createContext once we can assume React 16+
  14. var createNamedContext = function createNamedContext(name) {
  15. var context = createContext();
  16. context.displayName = name;
  17. return context;
  18. };
  19. var historyContext = /*#__PURE__*/createNamedContext("Router-History");
  20. var context = /*#__PURE__*/createNamedContext("Router");
  21. /**
  22. * The public API for putting history on context.
  23. */
  24. var Router = /*#__PURE__*/function (_React$Component) {
  25. _inheritsLoose(Router, _React$Component);
  26. Router.computeRootMatch = function computeRootMatch(pathname) {
  27. return {
  28. path: "/",
  29. url: "/",
  30. params: {},
  31. isExact: pathname === "/"
  32. };
  33. };
  34. function Router(props) {
  35. var _this;
  36. _this = _React$Component.call(this, props) || this;
  37. _this.state = {
  38. location: props.history.location
  39. }; // This is a bit of a hack. We have to start listening for location
  40. // changes here in the constructor in case there are any <Redirect>s
  41. // on the initial render. If there are, they will replace/push when
  42. // they mount and since cDM fires in children before parents, we may
  43. // get a new location before the <Router> is mounted.
  44. _this._isMounted = false;
  45. _this._pendingLocation = null;
  46. if (!props.staticContext) {
  47. _this.unlisten = props.history.listen(function (location) {
  48. if (_this._isMounted) {
  49. _this.setState({
  50. location: location
  51. });
  52. } else {
  53. _this._pendingLocation = location;
  54. }
  55. });
  56. }
  57. return _this;
  58. }
  59. var _proto = Router.prototype;
  60. _proto.componentDidMount = function componentDidMount() {
  61. this._isMounted = true;
  62. if (this._pendingLocation) {
  63. this.setState({
  64. location: this._pendingLocation
  65. });
  66. }
  67. };
  68. _proto.componentWillUnmount = function componentWillUnmount() {
  69. if (this.unlisten) {
  70. this.unlisten();
  71. this._isMounted = false;
  72. this._pendingLocation = null;
  73. }
  74. };
  75. _proto.render = function render() {
  76. return /*#__PURE__*/React.createElement(context.Provider, {
  77. value: {
  78. history: this.props.history,
  79. location: this.state.location,
  80. match: Router.computeRootMatch(this.state.location.pathname),
  81. staticContext: this.props.staticContext
  82. }
  83. }, /*#__PURE__*/React.createElement(historyContext.Provider, {
  84. children: this.props.children || null,
  85. value: this.props.history
  86. }));
  87. };
  88. return Router;
  89. }(React.Component);
  90. if (process.env.NODE_ENV !== "production") {
  91. Router.propTypes = {
  92. children: PropTypes.node,
  93. history: PropTypes.object.isRequired,
  94. staticContext: PropTypes.object
  95. };
  96. Router.prototype.componentDidUpdate = function (prevProps) {
  97. process.env.NODE_ENV !== "production" ? warning(prevProps.history === this.props.history, "You cannot change <Router history>") : void 0;
  98. };
  99. }
  100. /**
  101. * The public API for a <Router> that stores location in memory.
  102. */
  103. var MemoryRouter = /*#__PURE__*/function (_React$Component) {
  104. _inheritsLoose(MemoryRouter, _React$Component);
  105. function MemoryRouter() {
  106. var _this;
  107. for (var _len = arguments.length, args = new Array(_len), _key = 0; _key < _len; _key++) {
  108. args[_key] = arguments[_key];
  109. }
  110. _this = _React$Component.call.apply(_React$Component, [this].concat(args)) || this;
  111. _this.history = createMemoryHistory(_this.props);
  112. return _this;
  113. }
  114. var _proto = MemoryRouter.prototype;
  115. _proto.render = function render() {
  116. return /*#__PURE__*/React.createElement(Router, {
  117. history: this.history,
  118. children: this.props.children
  119. });
  120. };
  121. return MemoryRouter;
  122. }(React.Component);
  123. if (process.env.NODE_ENV !== "production") {
  124. MemoryRouter.propTypes = {
  125. initialEntries: PropTypes.array,
  126. initialIndex: PropTypes.number,
  127. getUserConfirmation: PropTypes.func,
  128. keyLength: PropTypes.number,
  129. children: PropTypes.node
  130. };
  131. MemoryRouter.prototype.componentDidMount = function () {
  132. process.env.NODE_ENV !== "production" ? warning(!this.props.history, "<MemoryRouter> ignores the history prop. To use a custom history, " + "use `import { Router }` instead of `import { MemoryRouter as Router }`.") : void 0;
  133. };
  134. }
  135. var Lifecycle = /*#__PURE__*/function (_React$Component) {
  136. _inheritsLoose(Lifecycle, _React$Component);
  137. function Lifecycle() {
  138. return _React$Component.apply(this, arguments) || this;
  139. }
  140. var _proto = Lifecycle.prototype;
  141. _proto.componentDidMount = function componentDidMount() {
  142. if (this.props.onMount) this.props.onMount.call(this, this);
  143. };
  144. _proto.componentDidUpdate = function componentDidUpdate(prevProps) {
  145. if (this.props.onUpdate) this.props.onUpdate.call(this, this, prevProps);
  146. };
  147. _proto.componentWillUnmount = function componentWillUnmount() {
  148. if (this.props.onUnmount) this.props.onUnmount.call(this, this);
  149. };
  150. _proto.render = function render() {
  151. return null;
  152. };
  153. return Lifecycle;
  154. }(React.Component);
  155. /**
  156. * The public API for prompting the user before navigating away from a screen.
  157. */
  158. function Prompt(_ref) {
  159. var message = _ref.message,
  160. _ref$when = _ref.when,
  161. when = _ref$when === void 0 ? true : _ref$when;
  162. return /*#__PURE__*/React.createElement(context.Consumer, null, function (context) {
  163. !context ? process.env.NODE_ENV !== "production" ? invariant(false, "You should not use <Prompt> outside a <Router>") : invariant(false) : void 0;
  164. if (!when || context.staticContext) return null;
  165. var method = context.history.block;
  166. return /*#__PURE__*/React.createElement(Lifecycle, {
  167. onMount: function onMount(self) {
  168. self.release = method(message);
  169. },
  170. onUpdate: function onUpdate(self, prevProps) {
  171. if (prevProps.message !== message) {
  172. self.release();
  173. self.release = method(message);
  174. }
  175. },
  176. onUnmount: function onUnmount(self) {
  177. self.release();
  178. },
  179. message: message
  180. });
  181. });
  182. }
  183. if (process.env.NODE_ENV !== "production") {
  184. var messageType = PropTypes.oneOfType([PropTypes.func, PropTypes.string]);
  185. Prompt.propTypes = {
  186. when: PropTypes.bool,
  187. message: messageType.isRequired
  188. };
  189. }
  190. var cache = {};
  191. var cacheLimit = 10000;
  192. var cacheCount = 0;
  193. function compilePath(path) {
  194. if (cache[path]) return cache[path];
  195. var generator = pathToRegexp.compile(path);
  196. if (cacheCount < cacheLimit) {
  197. cache[path] = generator;
  198. cacheCount++;
  199. }
  200. return generator;
  201. }
  202. /**
  203. * Public API for generating a URL pathname from a path and parameters.
  204. */
  205. function generatePath(path, params) {
  206. if (path === void 0) {
  207. path = "/";
  208. }
  209. if (params === void 0) {
  210. params = {};
  211. }
  212. return path === "/" ? path : compilePath(path)(params, {
  213. pretty: true
  214. });
  215. }
  216. /**
  217. * The public API for navigating programmatically with a component.
  218. */
  219. function Redirect(_ref) {
  220. var computedMatch = _ref.computedMatch,
  221. to = _ref.to,
  222. _ref$push = _ref.push,
  223. push = _ref$push === void 0 ? false : _ref$push;
  224. return /*#__PURE__*/React.createElement(context.Consumer, null, function (context) {
  225. !context ? process.env.NODE_ENV !== "production" ? invariant(false, "You should not use <Redirect> outside a <Router>") : invariant(false) : void 0;
  226. var history = context.history,
  227. staticContext = context.staticContext;
  228. var method = push ? history.push : history.replace;
  229. var location = createLocation(computedMatch ? typeof to === "string" ? generatePath(to, computedMatch.params) : _extends({}, to, {
  230. pathname: generatePath(to.pathname, computedMatch.params)
  231. }) : to); // When rendering in a static context,
  232. // set the new location immediately.
  233. if (staticContext) {
  234. method(location);
  235. return null;
  236. }
  237. return /*#__PURE__*/React.createElement(Lifecycle, {
  238. onMount: function onMount() {
  239. method(location);
  240. },
  241. onUpdate: function onUpdate(self, prevProps) {
  242. var prevLocation = createLocation(prevProps.to);
  243. if (!locationsAreEqual(prevLocation, _extends({}, location, {
  244. key: prevLocation.key
  245. }))) {
  246. method(location);
  247. }
  248. },
  249. to: to
  250. });
  251. });
  252. }
  253. if (process.env.NODE_ENV !== "production") {
  254. Redirect.propTypes = {
  255. push: PropTypes.bool,
  256. from: PropTypes.string,
  257. to: PropTypes.oneOfType([PropTypes.string, PropTypes.object]).isRequired
  258. };
  259. }
  260. var cache$1 = {};
  261. var cacheLimit$1 = 10000;
  262. var cacheCount$1 = 0;
  263. function compilePath$1(path, options) {
  264. var cacheKey = "" + options.end + options.strict + options.sensitive;
  265. var pathCache = cache$1[cacheKey] || (cache$1[cacheKey] = {});
  266. if (pathCache[path]) return pathCache[path];
  267. var keys = [];
  268. var regexp = pathToRegexp(path, keys, options);
  269. var result = {
  270. regexp: regexp,
  271. keys: keys
  272. };
  273. if (cacheCount$1 < cacheLimit$1) {
  274. pathCache[path] = result;
  275. cacheCount$1++;
  276. }
  277. return result;
  278. }
  279. /**
  280. * Public API for matching a URL pathname to a path.
  281. */
  282. function matchPath(pathname, options) {
  283. if (options === void 0) {
  284. options = {};
  285. }
  286. if (typeof options === "string" || Array.isArray(options)) {
  287. options = {
  288. path: options
  289. };
  290. }
  291. var _options = options,
  292. path = _options.path,
  293. _options$exact = _options.exact,
  294. exact = _options$exact === void 0 ? false : _options$exact,
  295. _options$strict = _options.strict,
  296. strict = _options$strict === void 0 ? false : _options$strict,
  297. _options$sensitive = _options.sensitive,
  298. sensitive = _options$sensitive === void 0 ? false : _options$sensitive;
  299. var paths = [].concat(path);
  300. return paths.reduce(function (matched, path) {
  301. if (!path && path !== "") return null;
  302. if (matched) return matched;
  303. var _compilePath = compilePath$1(path, {
  304. end: exact,
  305. strict: strict,
  306. sensitive: sensitive
  307. }),
  308. regexp = _compilePath.regexp,
  309. keys = _compilePath.keys;
  310. var match = regexp.exec(pathname);
  311. if (!match) return null;
  312. var url = match[0],
  313. values = match.slice(1);
  314. var isExact = pathname === url;
  315. if (exact && !isExact) return null;
  316. return {
  317. path: path,
  318. // the path used to match
  319. url: path === "/" && url === "" ? "/" : url,
  320. // the matched portion of the URL
  321. isExact: isExact,
  322. // whether or not we matched exactly
  323. params: keys.reduce(function (memo, key, index) {
  324. memo[key.name] = values[index];
  325. return memo;
  326. }, {})
  327. };
  328. }, null);
  329. }
  330. function isEmptyChildren(children) {
  331. return React.Children.count(children) === 0;
  332. }
  333. function evalChildrenDev(children, props, path) {
  334. var value = children(props);
  335. process.env.NODE_ENV !== "production" ? warning(value !== undefined, "You returned `undefined` from the `children` function of " + ("<Route" + (path ? " path=\"" + path + "\"" : "") + ">, but you ") + "should have returned a React element or `null`") : void 0;
  336. return value || null;
  337. }
  338. /**
  339. * The public API for matching a single path and rendering.
  340. */
  341. var Route = /*#__PURE__*/function (_React$Component) {
  342. _inheritsLoose(Route, _React$Component);
  343. function Route() {
  344. return _React$Component.apply(this, arguments) || this;
  345. }
  346. var _proto = Route.prototype;
  347. _proto.render = function render() {
  348. var _this = this;
  349. return /*#__PURE__*/React.createElement(context.Consumer, null, function (context$1) {
  350. !context$1 ? process.env.NODE_ENV !== "production" ? invariant(false, "You should not use <Route> outside a <Router>") : invariant(false) : void 0;
  351. var location = _this.props.location || context$1.location;
  352. var match = _this.props.computedMatch ? _this.props.computedMatch // <Switch> already computed the match for us
  353. : _this.props.path ? matchPath(location.pathname, _this.props) : context$1.match;
  354. var props = _extends({}, context$1, {
  355. location: location,
  356. match: match
  357. });
  358. var _this$props = _this.props,
  359. children = _this$props.children,
  360. component = _this$props.component,
  361. render = _this$props.render; // Preact uses an empty array as children by
  362. // default, so use null if that's the case.
  363. if (Array.isArray(children) && isEmptyChildren(children)) {
  364. children = null;
  365. }
  366. return /*#__PURE__*/React.createElement(context.Provider, {
  367. value: props
  368. }, props.match ? children ? typeof children === "function" ? process.env.NODE_ENV !== "production" ? evalChildrenDev(children, props, _this.props.path) : children(props) : children : component ? /*#__PURE__*/React.createElement(component, props) : render ? render(props) : null : typeof children === "function" ? process.env.NODE_ENV !== "production" ? evalChildrenDev(children, props, _this.props.path) : children(props) : null);
  369. });
  370. };
  371. return Route;
  372. }(React.Component);
  373. if (process.env.NODE_ENV !== "production") {
  374. Route.propTypes = {
  375. children: PropTypes.oneOfType([PropTypes.func, PropTypes.node]),
  376. component: function component(props, propName) {
  377. if (props[propName] && !isValidElementType(props[propName])) {
  378. return new Error("Invalid prop 'component' supplied to 'Route': the prop is not a valid React component");
  379. }
  380. },
  381. exact: PropTypes.bool,
  382. location: PropTypes.object,
  383. path: PropTypes.oneOfType([PropTypes.string, PropTypes.arrayOf(PropTypes.string)]),
  384. render: PropTypes.func,
  385. sensitive: PropTypes.bool,
  386. strict: PropTypes.bool
  387. };
  388. Route.prototype.componentDidMount = function () {
  389. process.env.NODE_ENV !== "production" ? warning(!(this.props.children && !isEmptyChildren(this.props.children) && this.props.component), "You should not use <Route component> and <Route children> in the same route; <Route component> will be ignored") : void 0;
  390. process.env.NODE_ENV !== "production" ? warning(!(this.props.children && !isEmptyChildren(this.props.children) && this.props.render), "You should not use <Route render> and <Route children> in the same route; <Route render> will be ignored") : void 0;
  391. process.env.NODE_ENV !== "production" ? warning(!(this.props.component && this.props.render), "You should not use <Route component> and <Route render> in the same route; <Route render> will be ignored") : void 0;
  392. };
  393. Route.prototype.componentDidUpdate = function (prevProps) {
  394. process.env.NODE_ENV !== "production" ? warning(!(this.props.location && !prevProps.location), '<Route> elements should not change from uncontrolled to controlled (or vice versa). You initially used no "location" prop and then provided one on a subsequent render.') : void 0;
  395. process.env.NODE_ENV !== "production" ? warning(!(!this.props.location && prevProps.location), '<Route> elements should not change from controlled to uncontrolled (or vice versa). You provided a "location" prop initially but omitted it on a subsequent render.') : void 0;
  396. };
  397. }
  398. function addLeadingSlash(path) {
  399. return path.charAt(0) === "/" ? path : "/" + path;
  400. }
  401. function addBasename(basename, location) {
  402. if (!basename) return location;
  403. return _extends({}, location, {
  404. pathname: addLeadingSlash(basename) + location.pathname
  405. });
  406. }
  407. function stripBasename(basename, location) {
  408. if (!basename) return location;
  409. var base = addLeadingSlash(basename);
  410. if (location.pathname.indexOf(base) !== 0) return location;
  411. return _extends({}, location, {
  412. pathname: location.pathname.substr(base.length)
  413. });
  414. }
  415. function createURL(location) {
  416. return typeof location === "string" ? location : createPath(location);
  417. }
  418. function staticHandler(methodName) {
  419. return function () {
  420. process.env.NODE_ENV !== "production" ? invariant(false, "You cannot %s with <StaticRouter>", methodName) : invariant(false) ;
  421. };
  422. }
  423. function noop() {}
  424. /**
  425. * The public top-level API for a "static" <Router>, so-called because it
  426. * can't actually change the current location. Instead, it just records
  427. * location changes in a context object. Useful mainly in testing and
  428. * server-rendering scenarios.
  429. */
  430. var StaticRouter = /*#__PURE__*/function (_React$Component) {
  431. _inheritsLoose(StaticRouter, _React$Component);
  432. function StaticRouter() {
  433. var _this;
  434. for (var _len = arguments.length, args = new Array(_len), _key = 0; _key < _len; _key++) {
  435. args[_key] = arguments[_key];
  436. }
  437. _this = _React$Component.call.apply(_React$Component, [this].concat(args)) || this;
  438. _this.handlePush = function (location) {
  439. return _this.navigateTo(location, "PUSH");
  440. };
  441. _this.handleReplace = function (location) {
  442. return _this.navigateTo(location, "REPLACE");
  443. };
  444. _this.handleListen = function () {
  445. return noop;
  446. };
  447. _this.handleBlock = function () {
  448. return noop;
  449. };
  450. return _this;
  451. }
  452. var _proto = StaticRouter.prototype;
  453. _proto.navigateTo = function navigateTo(location, action) {
  454. var _this$props = this.props,
  455. _this$props$basename = _this$props.basename,
  456. basename = _this$props$basename === void 0 ? "" : _this$props$basename,
  457. _this$props$context = _this$props.context,
  458. context = _this$props$context === void 0 ? {} : _this$props$context;
  459. context.action = action;
  460. context.location = addBasename(basename, createLocation(location));
  461. context.url = createURL(context.location);
  462. };
  463. _proto.render = function render() {
  464. var _this$props2 = this.props,
  465. _this$props2$basename = _this$props2.basename,
  466. basename = _this$props2$basename === void 0 ? "" : _this$props2$basename,
  467. _this$props2$context = _this$props2.context,
  468. context = _this$props2$context === void 0 ? {} : _this$props2$context,
  469. _this$props2$location = _this$props2.location,
  470. location = _this$props2$location === void 0 ? "/" : _this$props2$location,
  471. rest = _objectWithoutPropertiesLoose(_this$props2, ["basename", "context", "location"]);
  472. var history = {
  473. createHref: function createHref(path) {
  474. return addLeadingSlash(basename + createURL(path));
  475. },
  476. action: "POP",
  477. location: stripBasename(basename, createLocation(location)),
  478. push: this.handlePush,
  479. replace: this.handleReplace,
  480. go: staticHandler("go"),
  481. goBack: staticHandler("goBack"),
  482. goForward: staticHandler("goForward"),
  483. listen: this.handleListen,
  484. block: this.handleBlock
  485. };
  486. return /*#__PURE__*/React.createElement(Router, _extends({}, rest, {
  487. history: history,
  488. staticContext: context
  489. }));
  490. };
  491. return StaticRouter;
  492. }(React.Component);
  493. if (process.env.NODE_ENV !== "production") {
  494. StaticRouter.propTypes = {
  495. basename: PropTypes.string,
  496. context: PropTypes.object,
  497. location: PropTypes.oneOfType([PropTypes.string, PropTypes.object])
  498. };
  499. StaticRouter.prototype.componentDidMount = function () {
  500. process.env.NODE_ENV !== "production" ? warning(!this.props.history, "<StaticRouter> ignores the history prop. To use a custom history, " + "use `import { Router }` instead of `import { StaticRouter as Router }`.") : void 0;
  501. };
  502. }
  503. /**
  504. * The public API for rendering the first <Route> that matches.
  505. */
  506. var Switch = /*#__PURE__*/function (_React$Component) {
  507. _inheritsLoose(Switch, _React$Component);
  508. function Switch() {
  509. return _React$Component.apply(this, arguments) || this;
  510. }
  511. var _proto = Switch.prototype;
  512. _proto.render = function render() {
  513. var _this = this;
  514. return /*#__PURE__*/React.createElement(context.Consumer, null, function (context) {
  515. !context ? process.env.NODE_ENV !== "production" ? invariant(false, "You should not use <Switch> outside a <Router>") : invariant(false) : void 0;
  516. var location = _this.props.location || context.location;
  517. var element, match; // We use React.Children.forEach instead of React.Children.toArray().find()
  518. // here because toArray adds keys to all child elements and we do not want
  519. // to trigger an unmount/remount for two <Route>s that render the same
  520. // component at different URLs.
  521. React.Children.forEach(_this.props.children, function (child) {
  522. if (match == null && /*#__PURE__*/React.isValidElement(child)) {
  523. element = child;
  524. var path = child.props.path || child.props.from;
  525. match = path ? matchPath(location.pathname, _extends({}, child.props, {
  526. path: path
  527. })) : context.match;
  528. }
  529. });
  530. return match ? /*#__PURE__*/React.cloneElement(element, {
  531. location: location,
  532. computedMatch: match
  533. }) : null;
  534. });
  535. };
  536. return Switch;
  537. }(React.Component);
  538. if (process.env.NODE_ENV !== "production") {
  539. Switch.propTypes = {
  540. children: PropTypes.node,
  541. location: PropTypes.object
  542. };
  543. Switch.prototype.componentDidUpdate = function (prevProps) {
  544. process.env.NODE_ENV !== "production" ? warning(!(this.props.location && !prevProps.location), '<Switch> elements should not change from uncontrolled to controlled (or vice versa). You initially used no "location" prop and then provided one on a subsequent render.') : void 0;
  545. process.env.NODE_ENV !== "production" ? warning(!(!this.props.location && prevProps.location), '<Switch> elements should not change from controlled to uncontrolled (or vice versa). You provided a "location" prop initially but omitted it on a subsequent render.') : void 0;
  546. };
  547. }
  548. /**
  549. * A public higher-order component to access the imperative API
  550. */
  551. function withRouter(Component) {
  552. var displayName = "withRouter(" + (Component.displayName || Component.name) + ")";
  553. var C = function C(props) {
  554. var wrappedComponentRef = props.wrappedComponentRef,
  555. remainingProps = _objectWithoutPropertiesLoose(props, ["wrappedComponentRef"]);
  556. return /*#__PURE__*/React.createElement(context.Consumer, null, function (context) {
  557. !context ? process.env.NODE_ENV !== "production" ? invariant(false, "You should not use <" + displayName + " /> outside a <Router>") : invariant(false) : void 0;
  558. return /*#__PURE__*/React.createElement(Component, _extends({}, remainingProps, context, {
  559. ref: wrappedComponentRef
  560. }));
  561. });
  562. };
  563. C.displayName = displayName;
  564. C.WrappedComponent = Component;
  565. if (process.env.NODE_ENV !== "production") {
  566. C.propTypes = {
  567. wrappedComponentRef: PropTypes.oneOfType([PropTypes.string, PropTypes.func, PropTypes.object])
  568. };
  569. }
  570. return hoistStatics(C, Component);
  571. }
  572. var useContext = React.useContext;
  573. function useHistory() {
  574. if (process.env.NODE_ENV !== "production") {
  575. !(typeof useContext === "function") ? process.env.NODE_ENV !== "production" ? invariant(false, "You must use React >= 16.8 in order to use useHistory()") : invariant(false) : void 0;
  576. }
  577. return useContext(historyContext);
  578. }
  579. function useLocation() {
  580. if (process.env.NODE_ENV !== "production") {
  581. !(typeof useContext === "function") ? process.env.NODE_ENV !== "production" ? invariant(false, "You must use React >= 16.8 in order to use useLocation()") : invariant(false) : void 0;
  582. }
  583. return useContext(context).location;
  584. }
  585. function useParams() {
  586. if (process.env.NODE_ENV !== "production") {
  587. !(typeof useContext === "function") ? process.env.NODE_ENV !== "production" ? invariant(false, "You must use React >= 16.8 in order to use useParams()") : invariant(false) : void 0;
  588. }
  589. var match = useContext(context).match;
  590. return match ? match.params : {};
  591. }
  592. function useRouteMatch(path) {
  593. if (process.env.NODE_ENV !== "production") {
  594. !(typeof useContext === "function") ? process.env.NODE_ENV !== "production" ? invariant(false, "You must use React >= 16.8 in order to use useRouteMatch()") : invariant(false) : void 0;
  595. }
  596. var location = useLocation();
  597. var match = useContext(context).match;
  598. return path ? matchPath(location.pathname, path) : match;
  599. }
  600. if (process.env.NODE_ENV !== "production") {
  601. if (typeof window !== "undefined") {
  602. var global = window;
  603. var key = "__react_router_build__";
  604. var buildNames = {
  605. cjs: "CommonJS",
  606. esm: "ES modules",
  607. umd: "UMD"
  608. };
  609. if (global[key] && global[key] !== "esm") {
  610. var initialBuildName = buildNames[global[key]];
  611. var secondaryBuildName = buildNames["esm"]; // TODO: Add link to article that explains in detail how to avoid
  612. // loading 2 different builds.
  613. throw new Error("You are loading the " + secondaryBuildName + " build of React Router " + ("on a page that is already running the " + initialBuildName + " ") + "build, so things won't work right.");
  614. }
  615. global[key] = "esm";
  616. }
  617. }
  618. export { MemoryRouter, Prompt, Redirect, Route, Router, StaticRouter, Switch, historyContext as __HistoryContext, context as __RouterContext, generatePath, matchPath, useHistory, useLocation, useParams, useRouteMatch, withRouter };
  619. //# sourceMappingURL=react-router.js.map