UNPKG

2.82 kBJavaScriptView Raw
1"use strict";
2
3Object.defineProperty(exports, "__esModule", {
4 value: true
5});
6exports.default = useLinkTo;
7var _core = require("@react-navigation/core");
8var React = _interopRequireWildcard(require("react"));
9var _LinkingContext = _interopRequireDefault(require("./LinkingContext"));
10function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
11function _getRequireWildcardCache(nodeInterop) { if (typeof WeakMap !== "function") return null; var cacheBabelInterop = new WeakMap(); var cacheNodeInterop = new WeakMap(); return (_getRequireWildcardCache = function (nodeInterop) { return nodeInterop ? cacheNodeInterop : cacheBabelInterop; })(nodeInterop); }
12function _interopRequireWildcard(obj, nodeInterop) { if (!nodeInterop && obj && obj.__esModule) { return obj; } if (obj === null || typeof obj !== "object" && typeof obj !== "function") { return { default: obj }; } var cache = _getRequireWildcardCache(nodeInterop); if (cache && cache.has(obj)) { return cache.get(obj); } var newObj = {}; var hasPropertyDescriptor = Object.defineProperty && Object.getOwnPropertyDescriptor; for (var key in obj) { if (key !== "default" && Object.prototype.hasOwnProperty.call(obj, key)) { var desc = hasPropertyDescriptor ? Object.getOwnPropertyDescriptor(obj, key) : null; if (desc && (desc.get || desc.set)) { Object.defineProperty(newObj, key, desc); } else { newObj[key] = obj[key]; } } } newObj.default = obj; if (cache) { cache.set(obj, newObj); } return newObj; }
13function useLinkTo() {
14 const navigation = React.useContext(_core.NavigationContainerRefContext);
15 const linking = React.useContext(_LinkingContext.default);
16 const linkTo = React.useCallback(to => {
17 if (navigation === undefined) {
18 throw new Error("Couldn't find a navigation object. Is your component inside NavigationContainer?");
19 }
20 if (typeof to !== 'string') {
21 // @ts-expect-error: This is fine
22 navigation.navigate(to.screen, to.params);
23 return;
24 }
25 if (!to.startsWith('/')) {
26 throw new Error(`The path must start with '/' (${to}).`);
27 }
28 const {
29 options
30 } = linking;
31 const state = options !== null && options !== void 0 && options.getStateFromPath ? options.getStateFromPath(to, options.config) : (0, _core.getStateFromPath)(to, options === null || options === void 0 ? void 0 : options.config);
32 if (state) {
33 const action = (0, _core.getActionFromState)(state, options === null || options === void 0 ? void 0 : options.config);
34 if (action !== undefined) {
35 navigation.dispatch(action);
36 } else {
37 navigation.reset(state);
38 }
39 } else {
40 throw new Error('Failed to parse the path to a navigation state.');
41 }
42 }, [linking, navigation]);
43 return linkTo;
44}
45//# sourceMappingURL=useLinkTo.js.map
\No newline at end of file