-
Notifications
You must be signed in to change notification settings - Fork 25
/
navigationService.js
51 lines (47 loc) · 1.57 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
/* eslint-disable fp/no-mutating-assign */
import { NavigationActions, StackActions } from '@react-navigation/compat';
/**
* 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
*/
const navigatorObject = {
navigator: null
};
/**
* This function is called when the RootScreen is created to set the navigator instance to use.
*/
export function setTopLevelNavigator(navigatorRef) {
Object.assign(navigatorObject, { 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.
*/
export function navigate(routeName, params) {
navigatorObject.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.
*/
export function navigateAndReset(routeName, params) {
navigatorObject.navigator.dispatch(
StackActions.replace({
routeName,
params
})
);
}