@rn-tools/navigation 2.2.1 → 2.2.3
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +51 -30
- package/package.json +1 -1
- package/src/deep-links.tsx +5 -9
package/README.md
CHANGED
|
@@ -530,19 +530,19 @@ let useUser = () => {
|
|
|
530
530
|
|
|
531
531
|
### Deep Links
|
|
532
532
|
|
|
533
|
-
This section will cover how to respond to deep links in your app. Deep links usually have some extra setup required - use Expo's [Deep Linking Guide](https://docs.expo.dev/guides/deep-linking/) to get started.
|
|
533
|
+
This section will cover how to respond to deep links in your app. Deep links usually have some extra setup required - use Expo's [Deep Linking Guide](https://docs.expo.dev/guides/deep-linking/) to get started. Once you are able to receive deep links, use the `DeepLinks` component exported from this library to handle them.
|
|
534
534
|
|
|
535
|
-
|
|
535
|
+
In this example we will have a basic 3 tab view. We want to repond to the link `home/items/:id` by navigating to the home tab and then pushing a detail screen with the corresponding item id. The deep link component takes an array of handlers which are functions that will be invoked when their `path` matches the deep link that was opened.
|
|
536
536
|
|
|
537
|
-
|
|
538
|
-
|
|
539
|
-
|
|
537
|
+
- Only the first matching handler will be invoked.
|
|
538
|
+
- The handler function will receive the params from the deep link - these use the same token syntax as libraries like `react-router` and `express` for path params.
|
|
539
|
+
- Make sure that the `DeepLinks` component is inside of a `Stack` component
|
|
540
540
|
|
|
541
541
|
```tsx
|
|
542
542
|
import { DeepLinks, navigation, Stack, Tabs } from "@rn-tools/navigation";
|
|
543
|
+
import * as Linking from "expo-linking";
|
|
543
544
|
import * as React from "react";
|
|
544
545
|
import { View, Text, TouchableOpacity } from "react-native";
|
|
545
|
-
import * as Linking from "expo-linking";
|
|
546
546
|
|
|
547
547
|
export function DeepLinksExample() {
|
|
548
548
|
// You'll likely want to use Expo's Linking API to get the current URL and path
|
|
@@ -550,32 +550,52 @@ export function DeepLinksExample() {
|
|
|
550
550
|
// let { path } = Linking.parse(url)
|
|
551
551
|
|
|
552
552
|
// But it's easier to test hardcoded strings for the sake of this example
|
|
553
|
-
let path = "/testing/home/item/
|
|
553
|
+
let path = "/testing/home/item/1";
|
|
554
554
|
|
|
555
555
|
return (
|
|
556
|
-
<
|
|
557
|
-
|
|
558
|
-
|
|
559
|
-
|
|
560
|
-
|
|
561
|
-
|
|
562
|
-
|
|
563
|
-
|
|
564
|
-
|
|
565
|
-
|
|
566
|
-
|
|
567
|
-
|
|
568
|
-
|
|
569
|
-
|
|
570
|
-
|
|
571
|
-
|
|
572
|
-
|
|
573
|
-
|
|
574
|
-
|
|
575
|
-
|
|
576
|
-
|
|
577
|
-
|
|
578
|
-
|
|
556
|
+
<Stack.Navigator
|
|
557
|
+
rootScreen={
|
|
558
|
+
<DeepLinks
|
|
559
|
+
path={path}
|
|
560
|
+
handlers={[
|
|
561
|
+
{
|
|
562
|
+
path: "/testing/home/item1/:itemId",
|
|
563
|
+
handler: (params: { itemId: string }) => {
|
|
564
|
+
let itemId = params.itemId;
|
|
565
|
+
|
|
566
|
+
// Go to home tab
|
|
567
|
+
navigation.setTabIndex(0);
|
|
568
|
+
|
|
569
|
+
// Push the screen we want
|
|
570
|
+
navigation.pushScreen(
|
|
571
|
+
<Stack.Screen>
|
|
572
|
+
<MyScreen title={`Item: ${itemId}`} />
|
|
573
|
+
</Stack.Screen>
|
|
574
|
+
);
|
|
575
|
+
},
|
|
576
|
+
},
|
|
577
|
+
{
|
|
578
|
+
path: "/testing/home/item/:itemId",
|
|
579
|
+
handler: (params: { itemId: string }) => {
|
|
580
|
+
let itemId = params.itemId;
|
|
581
|
+
|
|
582
|
+
// Go to home tab
|
|
583
|
+
navigation.setTabIndex(0);
|
|
584
|
+
|
|
585
|
+
// Push the screen we want
|
|
586
|
+
navigation.pushScreen(
|
|
587
|
+
<Stack.Screen>
|
|
588
|
+
<MyScreen title={`Item: ${itemId}`} />
|
|
589
|
+
</Stack.Screen>
|
|
590
|
+
);
|
|
591
|
+
},
|
|
592
|
+
},
|
|
593
|
+
]}
|
|
594
|
+
>
|
|
595
|
+
<MyTabs />
|
|
596
|
+
</DeepLinks>
|
|
597
|
+
}
|
|
598
|
+
/>
|
|
579
599
|
);
|
|
580
600
|
}
|
|
581
601
|
|
|
@@ -661,6 +681,7 @@ function MyScreen({
|
|
|
661
681
|
</View>
|
|
662
682
|
);
|
|
663
683
|
}
|
|
684
|
+
|
|
664
685
|
```
|
|
665
686
|
|
|
666
687
|
### Preventing going back
|
package/package.json
CHANGED
package/src/deep-links.tsx
CHANGED
|
@@ -19,15 +19,11 @@ type DeepLinksProps<T> = {
|
|
|
19
19
|
children: React.ReactNode;
|
|
20
20
|
};
|
|
21
21
|
|
|
22
|
-
export function DeepLinks<T>({ path, handlers, children }: DeepLinksProps<T>) {
|
|
23
|
-
let matchers = React.useRef(buildMatchers(handlers));
|
|
24
|
-
|
|
25
|
-
React.useLayoutEffect(() => {
|
|
26
|
-
matchers.current = buildMatchers(handlers);
|
|
27
|
-
}, [handlers]);
|
|
28
|
-
|
|
22
|
+
export function DeepLinks<T>({ path = '', handlers = [], children }: DeepLinksProps<T>) {
|
|
29
23
|
React.useEffect(() => {
|
|
30
|
-
|
|
24
|
+
let matchers = buildMatchers(handlers);
|
|
25
|
+
|
|
26
|
+
for (let matcher of matchers) {
|
|
31
27
|
let { fn, handler } = matcher;
|
|
32
28
|
let match = fn(path);
|
|
33
29
|
if (match) {
|
|
@@ -35,7 +31,7 @@ export function DeepLinks<T>({ path, handlers, children }: DeepLinksProps<T>) {
|
|
|
35
31
|
break;
|
|
36
32
|
}
|
|
37
33
|
}
|
|
38
|
-
}, [path]);
|
|
34
|
+
}, [path, handlers]);
|
|
39
35
|
|
|
40
36
|
return <>{children}</>;
|
|
41
37
|
}
|