react-router.js 24 KB

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