Skip to content

Commit 99f1ab3

Browse files
authored
refact: keep useLinking more focused on deep links
1 parent e58ece8 commit 99f1ab3

File tree

3 files changed

+4
-32
lines changed

3 files changed

+4
-32
lines changed

README.md

+2-2
Original file line numberDiff line numberDiff line change
@@ -156,12 +156,12 @@ const { isRefreshing, onRefresh } = useRefresh(fetch);
156156

157157
### `useLinking`
158158

159-
useLinking can handle incoming your app's deeplinks and opening external urls through the [`Linking`](https://reactnative.dev/docs/linking) API.
159+
useLinking can handle incoming your app's deeplinks through the [`Linking`](https://reactnative.dev/docs/linking) API.
160160

161161
```js
162162
import {useLinking} from '@react-native-community/hooks'
163163

164-
const { deepLink, openLinkInBrowser } = useLinking()
164+
const { deepLink } = useLinking()
165165

166166
console.log('Initial deep link is:', deepLink)
167167
```

src/useLinking.test.ts

-22
Original file line numberDiff line numberDiff line change
@@ -33,26 +33,4 @@ describe('useLinking', () => {
3333

3434
expect(result.current.deepLink).toBe('app://magic_screen')
3535
})
36-
37-
it('should open link in browser', async () => {
38-
const {result} = renderHook(() => useLinking())
39-
const url = 'https://reactnative.dev'
40-
41-
await act(async () => {
42-
result.current.openLinkInBrowser(url)
43-
})
44-
45-
expect(Linking.canOpenURL).toHaveBeenCalledWith(url)
46-
expect(Linking.openURL).toHaveBeenCalledWith(url)
47-
})
48-
49-
it('should open app settings', async () => {
50-
const {result} = renderHook(() => useLinking())
51-
52-
await act(async () => {
53-
result.current.openAppSettings()
54-
})
55-
56-
expect(Linking.openSettings).toHaveBeenCalled()
57-
})
5836
})

src/useLinking.ts

+2-8
Original file line numberDiff line numberDiff line change
@@ -4,18 +4,12 @@ import {Linking} from 'react-native'
44
const useLinking = () => {
55
const [deepLink, setDeepLink] = useState<string | null>(null)
66

7-
const openLinkInBrowser = (url: string) => {
8-
Linking.canOpenURL(url).then((canOpen) => canOpen && Linking.openURL(url))
9-
}
10-
11-
const openAppSettings = async () => await Linking.openSettings()
12-
137
const handleURLChange = (event: {url: string}) => {
148
setDeepLink(event.url)
159
}
1610

1711
useEffect(() => {
18-
Linking.getInitialURL().then((url) => setDeepLink(url))
12+
Linking.getInitialURL().then((url: string) => setDeepLink(url))
1913
}, [])
2014

2115
useEffect(() => {
@@ -24,7 +18,7 @@ const useLinking = () => {
2418
return () => listener.remove()
2519
}, [])
2620

27-
return {openLinkInBrowser, openAppSettings, deepLink}
21+
return {deepLink}
2822
}
2923

3024
export {useLinking}

0 commit comments

Comments
 (0)