Skip to content

Commit f74606e

Browse files
author
Matthieu Napoli
committed
Add documentation for the navigation
1 parent 1a20979 commit f74606e

File tree

4 files changed

+33
-7
lines changed

4 files changed

+33
-7
lines changed

App/Containers/Root/RootScreen.js

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,12 +16,14 @@ import StartupActions from 'App/Stores/Startup/Actions'
1616
const AppNav = createStackNavigator(
1717
{
1818
// Create the application routes here (the key is the route name, the value is the target screen)
19+
// See https://reactnavigation.org/docs/en/stack-navigator.html#routeconfigs
1920
SplashScreen: SplashScreen,
2021
MainScreen: ExampleScreen,
2122
},
2223
{
2324
// By default the application will show the splash screen
2425
initialRouteName: 'SplashScreen',
26+
// See https://reactnavigation.org/docs/en/stack-navigator.html#stacknavigatorconfig
2527
headerMode: 'none',
2628
}
2729
)
@@ -36,6 +38,7 @@ class RootScreen extends Component {
3638
return (
3739
<View style={styles.container}>
3840
<AppNav
41+
// Initialize the NavigationService (see https://reactnavigation.org/docs/en/navigating-without-navigation-prop.html)
3942
ref={(navigatorRef) => {
4043
NavigationService.setTopLevelNavigator(navigatorRef)
4144
}}

App/Services/NavigationService.js

Lines changed: 28 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,22 +1,46 @@
11
import { NavigationActions, StackActions } from 'react-navigation'
22

3-
let _navigator
3+
/**
4+
* The navigation is implemented as a service so that it can be used outside of components, for example in sagas.
5+
*
6+
* @see https://reactnavigation.org/docs/en/navigating-without-navigation-prop.html
7+
*/
48

9+
let navigator
10+
11+
/**
12+
* This function is called when the RootScreen is created to set the navigator instance to use.
13+
*/
514
function setTopLevelNavigator(navigatorRef) {
6-
_navigator = navigatorRef
15+
navigator = navigatorRef
716
}
817

18+
/**
19+
* Call this function when you want to navigate to a specific route.
20+
*
21+
* @param routeName The name of the route to navigate to. Routes are defined in RootScreen using createStackNavigator()
22+
* @param params Route parameters.
23+
*/
924
function navigate(routeName, params) {
10-
_navigator.dispatch(
25+
navigator.dispatch(
1126
NavigationActions.navigate({
1227
routeName,
1328
params,
1429
})
1530
)
1631
}
1732

33+
/**
34+
* Call this function when you want to navigate to a specific route AND reset the navigation history.
35+
*
36+
* That means the user cannot go back. This is useful for example to redirect from a splashscreen to
37+
* the main screen: the user should not be able to go back to the splashscreen.
38+
*
39+
* @param routeName The name of the route to navigate to. Routes are defined in RootScreen using createStackNavigator()
40+
* @param params Route parameters.
41+
*/
1842
function navigateAndReset(routeName, params) {
19-
_navigator.dispatch(
43+
navigator.dispatch(
2044
StackActions.reset({
2145
index: 0,
2246
key: null,
@@ -30,7 +54,6 @@ function navigateAndReset(routeName, params) {
3054
)
3155
}
3256

33-
// add other navigation functions that you need and export them
3457
export default {
3558
navigate,
3659
navigateAndReset,

App/Services/README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
This directory contains services that are meant to connect the application to other APIs, for example an API client for Yahoo's weather service.
1+
This directory contains application services, for example services to connect the application to APIs.

README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ The boilerplate contains:
1616
- [reduxsauce](https://github.com/infinitered/reduxsauce) (v0.7) to facilitate using Redux
1717
- [apisauce](https://github.com/infinitered/apisauce) (v0.15) to make [axios](https://github.com/axios/axios) even better
1818
- [prettier](https://prettier.io/) and [eslint](https://eslint.org/) preconfigured for React Native
19-
- [react-native-navigation](https://reactnavigation.org) (v2.12) for the application's navigation
19+
- [react-native-navigation](https://reactnavigation.org) (v2.12) with a [`NavigationService`](App/Services/NavigationService.js) for the application's navigation
2020

2121
## Updates
2222

0 commit comments

Comments
 (0)