react-navi
Version:
A batteries-included router for react.
81 lines • 3.38 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 { createBrowserNavigation } from 'navi';
import { NaviProvider } from './NaviProvider';
import { View } from './View';
var Router = /** @class */ (function (_super) {
__extends(Router, _super);
function Router(props) {
var _this = _super.call(this, props) || this;
if (process.env.NODE_ENV !== 'production' && props.navigation) {
if (props.basename) {
console.warn("Warning: <Router> can't receive both a \"basename\" and a \"navigation\" prop. Proceeding by ignoring \"basename\".");
}
if (props.routes) {
console.warn("Warning: <Router> can't receive both a \"routes\" and a \"navigation\" prop. Proceeding by ignoring \"routes\".");
}
if (props.history) {
console.warn("Warning: <Router> can't receive both a \"history\" and a \"navigation\" prop. Proceeding by ignoring \"history\".");
}
}
_this.navigation =
props.navigation ||
createBrowserNavigation({
basename: props.basename,
context: props.context,
history: props.history,
routes: props.routes,
});
return _this;
}
Router.prototype.render = function () {
var _a = this.props, children = _a.children, hashScrollBehavior = _a.hashScrollBehavior;
return (React.createElement(NaviProvider, { navigation: this.navigation, hashScrollBehavior: hashScrollBehavior }, children || React.createElement(View, null)));
};
Router.prototype.componentDidMount = function () {
if (this.props.navigation && this.props.context) {
this.props.navigation.setContext(this.props.context);
}
};
Router.prototype.componentDidUpdate = function (prevProps) {
if (shallowDiffers(prevProps.context || {}, this.props.context || {})) {
this.navigation.setContext(this.props.context || {});
}
};
Router.prototype.componentWillUnmount = function () {
// Clean up any navigation object that we've created.
if (!this.props.navigation) {
this.navigation.dispose();
}
delete this.navigation;
};
Router.defaultProps = {
fallback: undefined,
};
return Router;
}(React.Component));
export { Router };
// Pulled from react-compat
// https://github.com/developit/preact-compat/blob/7c5de00e7c85e2ffd011bf3af02899b63f699d3a/src/index.js#L349
function shallowDiffers(a, b) {
for (var i in a)
if (!(i in b))
return true;
for (var i in b)
if (a[i] !== b[i])
return true;
return false;
}
//# sourceMappingURL=Router.js.map