UNPKG

3.8 kBJavaScriptView Raw
1var _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
3function _classCallCheck(instance, Constructor) { if (!(instance instanceof Constructor)) { throw new TypeError("Cannot call a class as a function"); } }
4
5function _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; }
6
7function _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; }
8
9import warning from 'warning';
10import invariant from 'invariant';
11import React from 'react';
12import PropTypes from 'prop-types';
13
14/**
15 * The public API for putting history on context.
16 */
17
18var Router = function (_React$Component) {
19 _inherits(Router, _React$Component);
20
21 function Router() {
22 var _temp, _this, _ret;
23
24 _classCallCheck(this, Router);
25
26 for (var _len = arguments.length, args = Array(_len), _key = 0; _key < _len; _key++) {
27 args[_key] = arguments[_key];
28 }
29
30 return _ret = (_temp = (_this = _possibleConstructorReturn(this, _React$Component.call.apply(_React$Component, [this].concat(args))), _this), _this.state = {
31 match: _this.computeMatch(_this.props.history.location.pathname)
32 }, _temp), _possibleConstructorReturn(_this, _ret);
33 }
34
35 Router.prototype.getChildContext = function getChildContext() {
36 return {
37 router: _extends({}, this.context.router, {
38 history: this.props.history,
39 route: {
40 location: this.props.history.location,
41 match: this.state.match
42 }
43 })
44 };
45 };
46
47 Router.prototype.computeMatch = function computeMatch(pathname) {
48 return {
49 path: '/',
50 url: '/',
51 params: {},
52 isExact: pathname === '/'
53 };
54 };
55
56 Router.prototype.componentWillMount = function componentWillMount() {
57 var _this2 = this;
58
59 var _props = this.props,
60 children = _props.children,
61 history = _props.history;
62
63
64 invariant(children == null || React.Children.count(children) === 1, 'A <Router> may have only one child element');
65
66 // Do this here so we can setState when a <Redirect> changes the
67 // location in componentWillMount. This happens e.g. when doing
68 // server rendering using a <StaticRouter>.
69 this.unlisten = history.listen(function () {
70 _this2.setState({
71 match: _this2.computeMatch(history.location.pathname)
72 });
73 });
74 };
75
76 Router.prototype.componentWillReceiveProps = function componentWillReceiveProps(nextProps) {
77 warning(this.props.history === nextProps.history, 'You cannot change <Router history>');
78 };
79
80 Router.prototype.componentWillUnmount = function componentWillUnmount() {
81 this.unlisten();
82 };
83
84 Router.prototype.render = function render() {
85 var children = this.props.children;
86
87 return children ? React.Children.only(children) : null;
88 };
89
90 return Router;
91}(React.Component);
92
93Router.propTypes = {
94 history: PropTypes.object.isRequired,
95 children: PropTypes.node
96};
97Router.contextTypes = {
98 router: PropTypes.object
99};
100Router.childContextTypes = {
101 router: PropTypes.object.isRequired
102};
103
104
105export default Router;
\No newline at end of file