Skip to content

Commit 0d18b36

Browse files
authored
feat: add clearLink function to detour context (#32)
1 parent 2f6f3d9 commit 0d18b36

3 files changed

Lines changed: 40 additions & 18 deletions

File tree

README.md

Lines changed: 32 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -1,28 +1,32 @@
11
<img src="https://github.com/user-attachments/assets/c965b51b-7307-477a-8d22-9c9cd6da6231" alt="React Native Detour by Software Mansion" width="100%"/>
22

3-
### React Native Detour is SDK for handling deferred links in React Native
3+
# React Native Detour
44

5-
## Documentation
5+
SDK for handling deferred links in React Native.
66

7-
Check out our dedicated documentation page for info about this library, API reference and more: [https://docs.swmansion.com/detour/docs/](https://docs.swmansion.com/detour/docs/)
7+
## Create an account
88

9-
## Create account on platform
9+
You need a Detour account to generate app credentials and configure your links.
10+
Sign up here: [https://godetour.dev/auth/signup](https://godetour.dev/auth/signup)
1011

11-
Create account and configure your links: [https://godetour.dev/auth/signup](https://godetour.dev/auth/signup)
12+
## Quick links
13+
14+
- Documentation: [https://docs.swmansion.com/detour/docs/](https://docs.swmansion.com/detour/docs/)
15+
- Installation guide: [https://docs.swmansion.com/detour/docs/SDK/sdk-installation](https://docs.swmansion.com/detour/docs/SDK/sdk-installation)
1216

1317
## Installation
1418

15-
[https://docs.swmansion.com/detour/docs/SDK/sdk-installation](https://docs.swmansion.com/detour/docs/SDK/sdk-installation)
19+
### Package
1620

17-
npm:
21+
Install the SDK:
1822

1923
```sh
2024
npm install @swmansion/react-native-detour
2125
```
2226

23-
#### You need to install additional dependencies
27+
### Additional dependencies
2428

25-
npm:
29+
Install required peer dependencies:
2630

2731
```sh
2832
npm install expo-localization react-native-device-info expo-clipboard @react-native-async-storage/async-storage expo-application
@@ -32,7 +36,7 @@ npm install expo-localization react-native-device-info expo-clipboard @react-nat
3236
3337
## Usage
3438

35-
#### Initialize provider in root of your app
39+
### Initialize the provider
3640

3741
```js
3842
import { DetourProvider, type Config } from '@swmansion/react-native-detour';
@@ -44,15 +48,15 @@ const config: Config = {
4448
};
4549

4650
export default function RootLayout() {
47-
48-
return(
51+
return (
4952
<DetourProvider config={config}>
5053
<RootNavigator />
51-
</DetourProvider>)
54+
</DetourProvider>
55+
);
5256
}
5357
```
5458

55-
#### Example usage with Expo Router
59+
### Example (Expo Router)
5660

5761
```js
5862
import { useDetourContext } from '@swmansion/react-native-detour';
@@ -62,7 +66,7 @@ import { Redirect, Stack } from 'expo-router';
6266
SplashScreen.preventAutoHideAsync();
6367

6468
export function RootNavigator() {
65-
const { isLinkProcessed, linkUrl, linkRoute } = useDetourContext();
69+
const { isLinkProcessed, linkRoute, clearLink } = useDetourContext();
6670

6771
useEffect(() => {
6872
if (isLinkProcessed) {
@@ -75,6 +79,7 @@ export function RootNavigator() {
7579
}
7680

7781
if (linkRoute) {
82+
clearLink(); // avoid redirecting again when returning to this screen
7883
return <Redirect href={linkRoute} />;
7984
}
8085

@@ -84,11 +89,15 @@ export function RootNavigator() {
8489

8590
Learn more about usage from our [docs](https://docs.swmansion.com/detour/docs/SDK/sdk-usage)
8691

92+
## Clearing handled links
93+
94+
If your app redirects based on `linkRoute` (especially in entry screens), call `clearLink()` after handling the route. This prevents repeated redirects when the user returns to the same screen.
95+
8796
## Types
8897

8998
The package exposes several types to help you with type-checking in your own codebase.
9099

91-
**Config**
100+
### Config
92101

93102
This type is used to define the configuration object you pass to the DetourProvider.
94103

@@ -119,7 +128,7 @@ export type Config = {
119128
};
120129
```
121130

122-
**DetourContextType**
131+
### DetourContextType
123132

124133
This type represents the object returned by the useDetourContext hook, containing the deferred link and its processing status.
125134

@@ -145,12 +154,17 @@ export type DetourContextType = {
145154
* The type of the detected link. Can be 'deferred', 'verified' or 'scheme'. This can be null if no link was found.
146155
*/
147156
linkType: LinkType | null;
157+
158+
/**
159+
* Clears the current link context (route/url/type). Call this after you handle a link.
160+
*/
161+
clearLink: () => void;
148162
};
149163
```
150164

151165
---
152166

153-
## :balance_scale: License
167+
## License
154168

155169
This library is licensed under [The MIT License](./LICENSE).
156170

src/hooks/useDetour.ts

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,12 @@ export const useDetour = ({
2525
const [route, setRoute] = useState<string | null>(null);
2626
const [linkType, setLinkType] = useState<LinkType | null>(null);
2727

28+
const clearLink = useCallback(() => {
29+
setLinkUrl(null);
30+
setRoute(null);
31+
setLinkType(null);
32+
}, []);
33+
2834
// Unified helper for parsing any link (API or Native)
2935
const processLink = useCallback(
3036
async (rawLink: string, typeOverride?: LinkType) => {
@@ -153,5 +159,6 @@ export const useDetour = ({
153159
linkUrl: linkUrl,
154160
linkRoute: route,
155161
linkType,
162+
clearLink,
156163
};
157164
};

src/types/index.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ export type DetourContextType = {
1414
linkUrl: string | URL | null;
1515
linkType: LinkType | null;
1616
linkRoute: string | null;
17+
clearLink: () => void;
1718
};
1819

1920
export interface DetourStorage {

0 commit comments

Comments
 (0)