react-router.js 24 KB

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