UNPKG

bloom-layout

Version:
108 lines (78 loc) 3.65 kB
'use strict'; exports.__esModule = true; var _react = require('react'); var _react2 = _interopRequireDefault(_react); var _propTypes = require('prop-types'); var _propTypes2 = _interopRequireDefault(_propTypes); var _warning = require('warning'); var _warning2 = _interopRequireDefault(_warning); var _invariant = require('invariant'); var _invariant2 = _interopRequireDefault(_invariant); var _history = require('history'); function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; } 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; } /** * 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() { (0, _invariant2.default)(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 = (0, _history.createLocation)(prevProps.to); var nextTo = (0, _history.createLocation)(this.props.to); if ((0, _history.locationsAreEqual)(prevTo, nextTo)) { (0, _warning2.default)(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; }(_react2.default.Component); Redirect.propTypes = { push: _propTypes2.default.bool, from: _propTypes2.default.string, to: _propTypes2.default.oneOfType([_propTypes2.default.string, _propTypes2.default.object]).isRequired }; Redirect.defaultProps = { push: false }; Redirect.contextTypes = { router: _propTypes2.default.shape({ history: _propTypes2.default.shape({ push: _propTypes2.default.func.isRequired, replace: _propTypes2.default.func.isRequired }).isRequired, staticContext: _propTypes2.default.object }).isRequired }; exports.default = Redirect;