react-router.js 24 KB

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