UNPKG

@vve/react-router

Version:

react router for react-helper & with keep-alive

69 lines (68 loc) 1.92 kB
import PropTypes from 'prop-types'; import React from 'react'; import { polyfill } from 'react-lifecycles-compat'; import withRouter from "./with-router"; class Garden extends React.Component { constructor(props) { super(props); this.state = { history: props.history, Component: null }; this.block = this.block.bind(this); } componentDidMount() { var _this$props = this.props, history = _this$props.history, location = _this$props.location; history.block(this.block); this.block(location); // init. } static getDerivedStateFromProps(nextProps, prevState) { if (prevState.skipDerived) { return { skipDerived: false }; } return { Component: nextProps.watcher(prevState.history.location, false), fromLocationChange: false }; } block(location) { var watcher = this.props.watcher; var result = watcher(location, true); this.setState({ Component: result, skipDerived: true, fromLocationChange: true }); return typeof result === 'function' ? false : result; } render() { var children = this.props.children; var _this$state = this.state, Component = _this$state.Component, fromLocationChange = _this$state.fromLocationChange; if (typeof Component === 'function') { return /*#__PURE__*/React.createElement(Component, null); } if (!fromLocationChange && Component === false) { return null; } return children; } } if (process.env.NODE_ENV !== 'production') { Garden.propTypes = { value: PropTypes.shape({}), location: PropTypes.shape({}).isRequired, history: PropTypes.shape({ block: PropTypes.func.isRequired }).isRequired, watcher: PropTypes.func.isRequired, children: PropTypes.element.isRequired }; } export default withRouter(polyfill(Garden)); //# sourceMappingURL=garden.js.map