Вы не можете выбрать более 25 тем Темы должны начинаться с буквы или цифры, могут содержать дефисы(-) и должны содержать не более 35 символов.
 
 
 
 

105 строки
3.7 KiB

  1. var _extends = Object.assign || function (target) { for (var i = 1; i < arguments.length; i++) { var source = arguments[i]; for (var key in source) { if (Object.prototype.hasOwnProperty.call(source, key)) { target[key] = source[key]; } } } return target; };
  2. function _classCallCheck(instance, Constructor) { if (!(instance instanceof Constructor)) { throw new TypeError("Cannot call a class as a function"); } }
  3. function _possibleConstructorReturn(self, call) { if (!self) { throw new ReferenceError("this hasn't been initialised - super() hasn't been called"); } return call && (typeof call === "object" || typeof call === "function") ? call : self; }
  4. function _inherits(subClass, superClass) { if (typeof superClass !== "function" && superClass !== null) { throw new TypeError("Super expression must either be null or a function, not " + typeof superClass); } subClass.prototype = Object.create(superClass && superClass.prototype, { constructor: { value: subClass, enumerable: false, writable: true, configurable: true } }); if (superClass) Object.setPrototypeOf ? Object.setPrototypeOf(subClass, superClass) : subClass.__proto__ = superClass; }
  5. import warning from "warning";
  6. import invariant from "invariant";
  7. import React from "react";
  8. import PropTypes from "prop-types";
  9. /**
  10. * The public API for putting history on context.
  11. */
  12. var Router = function (_React$Component) {
  13. _inherits(Router, _React$Component);
  14. function Router() {
  15. var _temp, _this, _ret;
  16. _classCallCheck(this, Router);
  17. for (var _len = arguments.length, args = Array(_len), _key = 0; _key < _len; _key++) {
  18. args[_key] = arguments[_key];
  19. }
  20. return _ret = (_temp = (_this = _possibleConstructorReturn(this, _React$Component.call.apply(_React$Component, [this].concat(args))), _this), _this.state = {
  21. match: _this.computeMatch(_this.props.history.location.pathname)
  22. }, _temp), _possibleConstructorReturn(_this, _ret);
  23. }
  24. Router.prototype.getChildContext = function getChildContext() {
  25. return {
  26. router: _extends({}, this.context.router, {
  27. history: this.props.history,
  28. route: {
  29. location: this.props.history.location,
  30. match: this.state.match
  31. }
  32. })
  33. };
  34. };
  35. Router.prototype.computeMatch = function computeMatch(pathname) {
  36. return {
  37. path: "/",
  38. url: "/",
  39. params: {},
  40. isExact: pathname === "/"
  41. };
  42. };
  43. Router.prototype.componentWillMount = function componentWillMount() {
  44. var _this2 = this;
  45. var _props = this.props,
  46. children = _props.children,
  47. history = _props.history;
  48. invariant(children == null || React.Children.count(children) === 1, "A <Router> may have only one child element");
  49. // Do this here so we can setState when a <Redirect> changes the
  50. // location in componentWillMount. This happens e.g. when doing
  51. // server rendering using a <StaticRouter>.
  52. this.unlisten = history.listen(function () {
  53. _this2.setState({
  54. match: _this2.computeMatch(history.location.pathname)
  55. });
  56. });
  57. };
  58. Router.prototype.componentWillReceiveProps = function componentWillReceiveProps(nextProps) {
  59. warning(this.props.history === nextProps.history, "You cannot change <Router history>");
  60. };
  61. Router.prototype.componentWillUnmount = function componentWillUnmount() {
  62. this.unlisten();
  63. };
  64. Router.prototype.render = function render() {
  65. var children = this.props.children;
  66. return children ? React.Children.only(children) : null;
  67. };
  68. return Router;
  69. }(React.Component);
  70. Router.propTypes = {
  71. history: PropTypes.object.isRequired,
  72. children: PropTypes.node
  73. };
  74. Router.contextTypes = {
  75. router: PropTypes.object
  76. };
  77. Router.childContextTypes = {
  78. router: PropTypes.object.isRequired
  79. };
  80. export default Router;