react-navi
Version:
A batteries-included router for react.
88 lines • 3.29 kB
JavaScript
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 __());
};
})();
import * as React from 'react';
import { HashScroll } from './HashScroll';
import { NaviContext } from './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, { behavior: this.props.hashScrollBehavior },
React.createElement(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));
export { NaviProvider };
//# sourceMappingURL=NaviProvider.js.map