react-navi
Version:
A batteries-included router for react.
90 lines • 3.77 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 __());
};
})();
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 navi_1 = require("navi");
var NaviProvider_1 = require("./NaviProvider");
var View_1 = require("./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 ||
navi_1.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_1.NaviProvider, { navigation: this.navigation, hashScrollBehavior: hashScrollBehavior }, children || React.createElement(View_1.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));
exports.Router = 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