UNPKG

@vve/react-router

Version:

react router for react-helper & with keep-alive

112 lines (108 loc) 4.67 kB
import _objectSpread from "@babel/runtime/helpers/objectSpread2"; import React from 'react'; import { isValidElementType } from 'react-is'; import PropTypes from 'prop-types'; import invariant from 'tiny-invariant'; import warning from 'tiny-warning'; import RouterContext from "./router-context"; import matchPath from "./match-path"; function isEmptyChildren(children) { return React.Children.count(children) === 0; } /** * The public API for matching a single path and rendering. */ class Route extends React.Component { render() { var _this$props = this.props, givenLocation = _this$props.location, computedMatch = _this$props.computedMatch, path = _this$props.path, component = _this$props.component, render = _this$props.render, keepAlive = _this$props.keepAlive, rrcActive = _this$props.rrcActive; return /*#__PURE__*/React.createElement(RouterContext.Consumer, null, context => { invariant(context, 'You should not use <Route> outside a <Router>'); var location = givenLocation || context.location; var match = computedMatch || (path ? matchPath(location.pathname, this.props) : context.match); var props = _objectSpread(_objectSpread({}, context), {}, { location, match, rrcActive }); var children = this.props.children; // Preact uses an empty array as children by // default, so use null if that's the case. if (Array.isArray(children) && children.length === 0) { children = null; } if (typeof children === 'function') { children = children(props); if (children === undefined) { if (process.env.NODE_ENV !== 'production') { warning(false, 'You returned `undefined` from the `children` function of ' + `<Route${path ? ` path="${path}"` : ''}>, but you ` + 'should have returned a React element or `null`'); } children = null; } } var renderChildren = null; if (children && !isEmptyChildren(children)) { renderChildren = children; } else if (props.match || this.rendered && keepAlive) { props.match = props.match || this.memedMatch; if (component) { renderChildren = /*#__PURE__*/React.createElement(component, props); } else if (render) { renderChildren = render(props); } } if (renderChildren && keepAlive) { this.rendered = true; this.memedMatch = props.match; } return /*#__PURE__*/React.createElement(RouterContext.Provider, { value: props }, renderChildren); }); } } if (process.env.NODE_ENV !== 'production') { Route.propTypes = { children: PropTypes.oneOfType([PropTypes.func, PropTypes.node]), component: (props, propName) => { if (props[propName] && !isValidElementType(props[propName])) { return new Error("Invalid prop 'component' supplied to 'Route': the prop is not a valid React component"); } }, exact: PropTypes.bool, location: PropTypes.object, path: PropTypes.oneOfType([PropTypes.string, PropTypes.arrayOf(PropTypes.string)]), render: PropTypes.func, sensitive: PropTypes.bool, strict: PropTypes.bool, keepAlive: PropTypes.bool, rrcActive: PropTypes.bool }; Route.defaultProps = { keepAlive: false }; Route.prototype.componentDidMount = function () { warning(!(this.props.children && !isEmptyChildren(this.props.children) && this.props.component), 'You should not use <Route component> and <Route children> in the same route; <Route component> will be ignored'); warning(!(this.props.children && !isEmptyChildren(this.props.children) && this.props.render), 'You should not use <Route render> and <Route children> in the same route; <Route render> will be ignored'); warning(!(this.props.component && this.props.render), 'You should not use <Route component> and <Route render> in the same route; <Route render> will be ignored'); }; // Route.prototype.componentDidUpdate = function (prevProps) { // warning( // !(this.props.location && !prevProps.location), // '<Route> elements should not change from uncontrolled to controlled (or vice versa). You initially used no "location" prop and then provided one on a subsequent render.' // ); // // warning( // !(!this.props.location && prevProps.location), // '<Route> elements should not change from controlled to uncontrolled (or vice versa). You provided a "location" prop initially but omitted it on a subsequent render.' // ); // }; } export default Route; //# sourceMappingURL=route.js.map