UNPKG

3.13 kBJavaScriptView Raw
1function _classCallCheck(instance, Constructor) { if (!(instance instanceof Constructor)) { throw new TypeError("Cannot call a class as a function"); } }
2
3function _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
5function _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; }
6
7import React from 'react';
8import PropTypes from 'prop-types';
9import warning from 'warning';
10import invariant from 'invariant';
11import { createLocation, locationsAreEqual } from 'history';
12
13/**
14 * The public API for updating the location programmatically
15 * with a component.
16 */
17
18var Redirect = function (_React$Component) {
19 _inherits(Redirect, _React$Component);
20
21 function Redirect() {
22 _classCallCheck(this, Redirect);
23
24 return _possibleConstructorReturn(this, _React$Component.apply(this, arguments));
25 }
26
27 Redirect.prototype.isStatic = function isStatic() {
28 return this.context.router && this.context.router.staticContext;
29 };
30
31 Redirect.prototype.componentWillMount = function componentWillMount() {
32 invariant(this.context.router, 'You should not use <Redirect> outside a <Router>');
33
34 if (this.isStatic()) this.perform();
35 };
36
37 Redirect.prototype.componentDidMount = function componentDidMount() {
38 if (!this.isStatic()) this.perform();
39 };
40
41 Redirect.prototype.componentDidUpdate = function componentDidUpdate(prevProps) {
42 var prevTo = createLocation(prevProps.to);
43 var nextTo = createLocation(this.props.to);
44
45 if (locationsAreEqual(prevTo, nextTo)) {
46 warning(false, 'You tried to redirect to the same route you\'re currently on: ' + ('"' + nextTo.pathname + nextTo.search + '"'));
47 return;
48 }
49
50 this.perform();
51 };
52
53 Redirect.prototype.perform = function perform() {
54 var history = this.context.router.history;
55 var _props = this.props,
56 push = _props.push,
57 to = _props.to;
58
59
60 if (push) {
61 history.push(to);
62 } else {
63 history.replace(to);
64 }
65 };
66
67 Redirect.prototype.render = function render() {
68 return null;
69 };
70
71 return Redirect;
72}(React.Component);
73
74Redirect.propTypes = {
75 push: PropTypes.bool,
76 from: PropTypes.string,
77 to: PropTypes.oneOfType([PropTypes.string, PropTypes.object]).isRequired
78};
79Redirect.defaultProps = {
80 push: false
81};
82Redirect.contextTypes = {
83 router: PropTypes.shape({
84 history: PropTypes.shape({
85 push: PropTypes.func.isRequired,
86 replace: PropTypes.func.isRequired
87 }).isRequired,
88 staticContext: PropTypes.object
89 }).isRequired
90};
91
92
93export default Redirect;
\No newline at end of file