react-router.js 25 KB

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