Skip to content

Commit 237c111

Browse files
committed
Update NavigationService example for v3
I am trying to follow these two doc pages in conjunction: - https://reactnavigation.org/docs/en/app-containers.html - https://reactnavigation.org/docs/en/navigating-without-navigation-prop.html Though the "Navigating without the navigation prop" seems out of date since the introduction of explicit app containers in v3, the **App Containers** page indicates that I can get a ref on the _container_ that functions just like a navigator. After a couple of false starts, I stumbled upon what seems to be the correct setup, but it would have been helpful to start from a working example without having to infer the correct combination. My minimal working example: https://snack.expo.io/@brettdh/navigationservice-and-createappcontainer Possibly related: react-navigation/react-navigation#5252
1 parent 6d5b6ff commit 237c111

File tree

2 files changed

+82
-2
lines changed

2 files changed

+82
-2
lines changed

docs/navigating-without-navigation-prop.md

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,16 +11,19 @@ You can get access to a navigator through a `ref` and pass it to the `Navigation
1111
```javascript
1212
// App.js
1313

14+
import { createStackNavigator, createAppContainer } from 'react-navigation';
1415
import NavigationService from './NavigationService';
1516

1617
const TopLevelNavigator = createStackNavigator({ /* ... */ })
1718

18-
class App extends React.Component {
19+
const AppContainer = createAppContainer(TopLevelNavigator);
20+
21+
export default class App extends React.Component {
1922
// ...
2023

2124
render() {
2225
return (
23-
<TopLevelNavigator
26+
<AppContainer
2427
ref={navigatorRef => {
2528
NavigationService.setTopLevelNavigator(navigatorRef);
2629
}}
Lines changed: 77 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,77 @@
1+
---
2+
id: navigating-without-navigation-prop
3+
title: Navigating without the navigation prop
4+
sidebar_label: Navigating without the navigation prop
5+
---
6+
7+
Calling functions such as `navigate` or `popToTop` on the `navigation` prop is not the only way to navigate around your app. As an alternative, you can dispatch navigation actions on your top-level navigator, provided you aren't passing your own `navigation` prop as you would with a redux integration. The presented approach is useful in situations when you want to trigger a navigation action from places where you do not have access to the `navigation` prop, or if you're looking for an alternative to using the `navigation` prop.
8+
9+
You can get access to a navigator through a `ref` and pass it to the `NavigationService` which we will later use to navigate. Use this only with the top-level (root) navigator of your app.
10+
11+
```javascript
12+
// App.js
13+
14+
import { createStackNavigator, createAppContainer } from 'react-navigation';
15+
import NavigationService from './NavigationService';
16+
17+
const TopLevelNavigator = createStackNavigator({ /* ... */ })
18+
19+
const AppContainer = createAppContainer(TopLevelNavigator);
20+
21+
export default class App extends React.Component {
22+
// ...
23+
24+
render() {
25+
return (
26+
<AppContainer
27+
ref={navigatorRef => {
28+
NavigationService.setTopLevelNavigator(navigatorRef);
29+
}}
30+
/>
31+
);
32+
}
33+
}
34+
```
35+
36+
In the next step, we define `NavigationService` which is a simple module with functions that dispatch user-defined navigation actions.
37+
38+
```javascript
39+
// NavigationService.js
40+
41+
import { NavigationActions } from 'react-navigation';
42+
43+
let _navigator;
44+
45+
function setTopLevelNavigator(navigatorRef) {
46+
_navigator = navigatorRef;
47+
}
48+
49+
function navigate(routeName, params) {
50+
_navigator.dispatch(
51+
NavigationActions.navigate({
52+
routeName,
53+
params,
54+
})
55+
);
56+
}
57+
58+
// add other navigation functions that you need and export them
59+
60+
export default {
61+
navigate,
62+
setTopLevelNavigator,
63+
};
64+
```
65+
66+
Then, in any of your javascript modules, just import the `NavigationService` and call functions which you exported from it. You may use this approach outside of your React components and, in fact, it works just as well when used from within them.
67+
68+
```javascript
69+
// any js module
70+
import NavigationService from 'path-to-NavigationService.js';
71+
72+
// ...
73+
74+
NavigationService.navigate('ChatScreen', { userName: 'Lucy' });
75+
```
76+
77+
In `NavigationService`, you can create your own navigation actions, or compose multiple navigation actions into one, and then easily reuse them throughout your application. When writing tests, you may mock the navigation functions, and make assertions on whether the correct functions are called, with the correct parameters.

0 commit comments

Comments
 (0)