-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathuseAppState.ts
More file actions
47 lines (39 loc) · 1.2 KB
/
Copy pathuseAppState.ts
File metadata and controls
47 lines (39 loc) · 1.2 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
/**
* @AngelaMos | 2026
* useAppState.ts
*/
import { focusManager } from '@tanstack/react-query'
import { useEffect } from 'react'
import { AppState, type AppStateStatus } from 'react-native'
interface UseAppStateOptions {
onForeground?: () => void | Promise<void>
onBackground?: () => void | Promise<void>
}
export const useAppState = (options?: UseAppStateOptions): void => {
useEffect(() => {
const handleAppStateChange = (nextAppState: AppStateStatus): void => {
if (nextAppState === 'active') {
focusManager.setFocused(true)
options?.onForeground?.()
} else {
focusManager.setFocused(false)
options?.onBackground?.()
}
}
const subscription = AppState.addEventListener('change', handleAppStateChange)
return (): void => {
subscription.remove()
}
}, [options])
}
export const useAppStateFocusManager = (): void => {
useEffect(() => {
const handleAppStateChange = (nextAppState: AppStateStatus): void => {
focusManager.setFocused(nextAppState === 'active')
}
const subscription = AppState.addEventListener('change', handleAppStateChange)
return (): void => {
subscription.remove()
}
}, [])
}