forked from thecodingmachine/react-native-boilerplate
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathNavigationService.js
61 lines (55 loc) · 1.59 KB
/
NavigationService.js
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
import { NavigationActions, StackActions } from 'react-navigation'
/**
* The navigation is implemented as a service so that it can be used outside of components, for example in sagas.
*
* @see https://reactnavigation.org/docs/en/navigating-without-navigation-prop.html
*/
let navigator
/**
* This function is called when the RootScreen is created to set the navigator instance to use.
*/
function setTopLevelNavigator(navigatorRef) {
navigator = navigatorRef
}
/**
* Call this function when you want to navigate to a specific route.
*
* @param routeName The name of the route to navigate to. Routes are defined in RootScreen using createStackNavigator()
* @param params Route parameters.
*/
function navigate(routeName, params) {
navigator.dispatch(
NavigationActions.navigate({
routeName,
params,
})
)
}
/**
* Call this function when you want to navigate to a specific route AND reset the navigation history.
*
* That means the user cannot go back. This is useful for example to redirect from a splashscreen to
* the main screen: the user should not be able to go back to the splashscreen.
*
* @param routeName The name of the route to navigate to. Routes are defined in RootScreen using createStackNavigator()
* @param params Route parameters.
*/
function navigateAndReset(routeName, params) {
navigator.dispatch(
StackActions.reset({
index: 0,
key: null,
actions: [
NavigationActions.navigate({
routeName,
params,
}),
],
})
)
}
export default {
navigate,
navigateAndReset,
setTopLevelNavigator,
}