UNPKG

bloom-layout

Version:
93 lines (71 loc) 3.13 kB
function _classCallCheck(instance, Constructor) { if (!(instance instanceof Constructor)) { throw new TypeError("Cannot call a class as a function"); } } 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; } 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; } import React from 'react'; import PropTypes from 'prop-types'; import warning from 'warning'; import invariant from 'invariant'; import { createLocation, locationsAreEqual } from 'history'; /** * The public API for updating the location programmatically * with a component. */ var Redirect = function (_React$Component) { _inherits(Redirect, _React$Component); function Redirect() { _classCallCheck(this, Redirect); return _possibleConstructorReturn(this, _React$Component.apply(this, arguments)); } Redirect.prototype.isStatic = function isStatic() { return this.context.router && this.context.router.staticContext; }; Redirect.prototype.componentWillMount = function componentWillMount() { invariant(this.context.router, 'You should not use <Redirect> outside a <Router>'); if (this.isStatic()) this.perform(); }; Redirect.prototype.componentDidMount = function componentDidMount() { if (!this.isStatic()) this.perform(); }; Redirect.prototype.componentDidUpdate = function componentDidUpdate(prevProps) { var prevTo = createLocation(prevProps.to); var nextTo = createLocation(this.props.to); if (locationsAreEqual(prevTo, nextTo)) { warning(false, 'You tried to redirect to the same route you\'re currently on: ' + ('"' + nextTo.pathname + nextTo.search + '"')); return; } this.perform(); }; Redirect.prototype.perform = function perform() { var history = this.context.router.history; var _props = this.props, push = _props.push, to = _props.to; if (push) { history.push(to); } else { history.replace(to); } }; Redirect.prototype.render = function render() { return null; }; return Redirect; }(React.Component); Redirect.propTypes = { push: PropTypes.bool, from: PropTypes.string, to: PropTypes.oneOfType([PropTypes.string, PropTypes.object]).isRequired }; Redirect.defaultProps = { push: false }; Redirect.contextTypes = { router: PropTypes.shape({ history: PropTypes.shape({ push: PropTypes.func.isRequired, replace: PropTypes.func.isRequired }).isRequired, staticContext: PropTypes.object }).isRequired }; export default Redirect;