UNPKG

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