-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathNavigationService.tsx
More file actions
98 lines (81 loc) · 2.6 KB
/
NavigationService.tsx
File metadata and controls
98 lines (81 loc) · 2.6 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
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
import {
createNavigationContainerRef,
NavigationAction,
NavigationProp,
NavigationState,
StackActions,
} from '@react-navigation/native'
import {AppStackParamList} from './types'
export const navigationRef = createNavigationContainerRef<AppStackParamList>()
export const canGoBack = (): boolean => navigationRef.isReady() && navigationRef.canGoBack()
export const getState = (): NavigationState | null =>
navigationRef.isReady() ? navigationRef.getState() : null
export const getParent = (): NavigationProp<AppStackParamList> | null =>
navigationRef.isReady() ? navigationRef.getParent() : null
export const navigate = (
name: keyof AppStackParamList,
params?: AppStackParamList[keyof AppStackParamList],
) => {
if (navigationRef.isReady()) {
navigationRef.navigate(name as string, params as object | undefined)
}
}
export const pop = (count?: number) => {
if (navigationRef.isReady()) {
navigationRef.dispatch(StackActions.pop(count))
}
}
export const popToTop = () => {
if (navigationRef.isReady()) {
navigationRef.dispatch(StackActions.popToTop())
}
}
export const push = (name: keyof AppStackParamList, params: AppStackParamList[keyof AppStackParamList]) => {
if (navigationRef.isReady()) {
navigationRef.dispatch(StackActions.push(name as string, params as object | undefined))
}
}
export const checkRouteOrigin = () => navigationRef.getRootState().routeNames[0]
export function navigationPop(numberToPop = 1) {
if (navigationRef.isReady()) {
navigationRef.dispatch(StackActions.pop(numberToPop))
}
}
export const replace = (
name: keyof AppStackParamList,
params: AppStackParamList[keyof AppStackParamList],
) => {
if (navigationRef.isReady()) {
navigationRef.dispatch(StackActions.replace(name as string, params as object | undefined))
}
}
export const reset = (state: NavigationState) => {
if (navigationRef.isReady()) {
navigationRef.reset(state)
}
}
export const resetTo = (name: string) => {
if (navigationRef.isReady()) {
navigationRef.reset({
index: 0,
routes: [{name}],
})
}
}
export const goBack = () => {
if (canGoBack()) {
navigationRef.goBack()
}
}
export const setParams = (params: AppStackParamList[keyof AppStackParamList]) => {
if (navigationRef.isReady()) {
navigationRef.setParams(params as object | undefined)
}
}
export const dispatch = (action: NavigationAction | ((state: NavigationState) => NavigationAction)) => {
if (navigationRef.isReady()) {
navigationRef.dispatch(action)
}
}
export const getCurrentRouteName = () =>
navigationRef.isReady() ? navigationRef?.getCurrentRoute()?.name : ''