@rn-tools/navigation 2.2.0 → 2.2.2
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 +6 -6
- package/package.json +1 -1
- package/src/deep-links.tsx +8 -10
package/README.md
CHANGED
|
@@ -530,12 +530,12 @@ 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
|
-
|
|
536
|
-
|
|
537
|
-
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. 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.
|
|
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.
|
|
538
536
|
|
|
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
539
|
|
|
540
540
|
```tsx
|
|
541
541
|
import { DeepLinks, navigation, Stack, Tabs } from "@rn-tools/navigation";
|
|
@@ -549,14 +549,14 @@ export function DeepLinksExample() {
|
|
|
549
549
|
// let { path } = Linking.parse(url)
|
|
550
550
|
|
|
551
551
|
// But it's easier to test hardcoded strings for the sake of this example
|
|
552
|
-
let path = "/
|
|
552
|
+
let path = "/home/item/4";
|
|
553
553
|
|
|
554
554
|
return (
|
|
555
555
|
<DeepLinks
|
|
556
556
|
path={path}
|
|
557
557
|
handlers={[
|
|
558
558
|
{
|
|
559
|
-
path: "/
|
|
559
|
+
path: "/home/item/:itemId",
|
|
560
560
|
handler: (params: { itemId: string }) => {
|
|
561
561
|
let itemId = params.itemId;
|
|
562
562
|
|
package/package.json
CHANGED
package/src/deep-links.tsx
CHANGED
|
@@ -20,20 +20,18 @@ type DeepLinksProps<T> = {
|
|
|
20
20
|
};
|
|
21
21
|
|
|
22
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
|
-
|
|
29
23
|
React.useEffect(() => {
|
|
30
|
-
matchers
|
|
24
|
+
let matchers = buildMatchers(handlers);
|
|
25
|
+
|
|
26
|
+
for (let matcher of matchers) {
|
|
27
|
+
let { fn, handler } = matcher;
|
|
31
28
|
let match = fn(path);
|
|
32
29
|
if (match) {
|
|
33
|
-
|
|
30
|
+
setImmediate(() => handler(match.params as T));
|
|
31
|
+
break;
|
|
34
32
|
}
|
|
35
|
-
}
|
|
36
|
-
}, [path]);
|
|
33
|
+
}
|
|
34
|
+
}, [path, handlers]);
|
|
37
35
|
|
|
38
36
|
return <>{children}</>;
|
|
39
37
|
}
|