UNPKG

react-router-4-compat

Version:

A wrapped version of react router 4 which compatible with react router 3

70 lines (59 loc) 1.88 kB
function _inheritsLoose(subClass, superClass) { subClass.prototype.__proto__ = superClass && superClass.prototype; subClass.__proto__ = superClass; } import React from 'react'; import PropTypes from 'prop-types'; import Switch from 'react-router/Switch'; import Router from 'react-router/Router'; import { castArray } from './utils'; import { createRoutes, RoutePropType } from './Route'; var Router4Compat = /*#__PURE__*/ function (_React$Component) { function Router4Compat(props, context) { var _this; // noinspection JSCheckFunctionSignatures _this = _React$Component.call(this, props, context) || this; _this.routesUpdater = function (routes) { _this.setState({ routes: routes }); }; _this.state = { routes: [] }; return _this; } var _proto = Router4Compat.prototype; _proto.getChildContext = function getChildContext() { return { routesCompat: this.state.routes, routesUpdater: this.routesUpdater }; }; _proto.render = function render() { var _this$props = this.props, routes = _this$props.routes, history = _this$props.history, onError = _this$props.onError; return React.createElement(Router, { history: history }, React.createElement(Switch, null, createRoutes(castArray(routes), onError, []))); }; _inheritsLoose(Router4Compat, _React$Component); return Router4Compat; }(React.Component); Router4Compat.propTypes = { history: PropTypes.object.isRequired, routes: PropTypes.oneOfType([RoutePropType, PropTypes.arrayOf(RoutePropType)]), onError: PropTypes.func }; Router4Compat.defaultProps = { routes: [], onError: function onError() { return null; } }; Router4Compat.childContextTypes = { routesCompat: PropTypes.arrayOf(RoutePropType), routesUpdater: PropTypes.func }; export default Router4Compat;