UNPKG

react-navi

Version:

A batteries-included router for react.

97 lines 3.7 kB
"use strict"; var __extends = (this && this.__extends) || (function () { var extendStatics = function (d, b) { extendStatics = Object.setPrototypeOf || ({ __proto__: [] } instanceof Array && function (d, b) { d.__proto__ = b; }) || function (d, b) { for (var p in b) if (b.hasOwnProperty(p)) d[p] = b[p]; }; return extendStatics(d, b); }; return function (d, b) { extendStatics(d, b); function __() { this.constructor = d; } d.prototype = b === null ? Object.create(b) : (__.prototype = b.prototype, new __()); }; })(); var __importStar = (this && this.__importStar) || function (mod) { if (mod && mod.__esModule) return mod; var result = {}; if (mod != null) for (var k in mod) if (Object.hasOwnProperty.call(mod, k)) result[k] = mod[k]; result["default"] = mod; return result; }; Object.defineProperty(exports, "__esModule", { value: true }); var React = __importStar(require("react")); var HashScroll_1 = require("./HashScroll"); var NaviContext_1 = require("./NaviContext"); var NaviProvider = /** @class */ (function (_super) { __extends(NaviProvider, _super); function NaviProvider(props) { var _this = _super.call(this, props) || this; _this.handleNavigationSnapshot = function (route) { if (route.type !== 'busy') { _this.setState({ steadyRoute: route, busyRoute: undefined, }); } else { _this.setState({ busyRoute: route, }); } }; _this.handleError = function (error) { throw error; }; _this.state = {}; return _this; } NaviProvider.getDerivedStateFromProps = function (props, state) { if (state.navigation !== props.navigation) { var route = props.navigation.getCurrentValue(); return route.type === 'busy' ? { steadyRoute: state.steadyRoute, busyRoute: route, navigation: props.navigation, } : { steadyRoute: route, busyRoute: undefined, navigation: props.navigation, }; } return null; }; NaviProvider.prototype.render = function () { return (React.createElement(HashScroll_1.HashScroll, { behavior: this.props.hashScrollBehavior }, React.createElement(NaviContext_1.NaviContext.Provider, { value: this.state }, this.props.children))); }; NaviProvider.prototype.componentDidMount = function () { this.subscribe(); }; NaviProvider.prototype.componentDidUpdate = function (prevProps) { if (prevProps.navigation !== this.props.navigation) { this.unsubscribe(); this.subscribe(); } }; NaviProvider.prototype.componentWillUnmount = function () { this.unsubscribe(); }; NaviProvider.prototype.subscribe = function () { if (!this.props.navigation) { throw new Error("A <NaviProvider> component must receive a \"navigation\" prop."); } this.subscription = this.props.navigation.subscribe(this.handleNavigationSnapshot, this.handleError); }; NaviProvider.prototype.unsubscribe = function () { if (this.subscription) { this.subscription.unsubscribe(); delete this.subscription; } }; return NaviProvider; }(React.Component)); exports.NaviProvider = NaviProvider; //# sourceMappingURL=NaviProvider.js.map