Skip to content

Commit ee178b6

Browse files
authored
Merge pull request react-navigation#85 from react-navigation/navigatorRef
document the top-level navigator ref
2 parents a2e1cb2 + b5e18a4 commit ee178b6

File tree

2 files changed

+76
-0
lines changed

2 files changed

+76
-0
lines changed
Lines changed: 75 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,75 @@
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 NavigationService from './NavigationService';
15+
16+
const TopLevelNavigator = StackNavigator({ /* ... */ })
17+
18+
class App extends React.Component {
19+
// ...
20+
21+
render(): {
22+
return (
23+
<TopLevelNavigator
24+
ref={navigatorRef => {
25+
NavigationService.setTopLevelNavigator(navigatorRef);
26+
}}
27+
/>
28+
);
29+
}
30+
}
31+
```
32+
33+
In the next step, we define `NavigationService` which is a simple module with functions that dispatch user-defined navigation actions.
34+
35+
```javascript
36+
// NavigationService.js
37+
38+
import { NavigationActions } from 'react-navigation';
39+
40+
let _navigator;
41+
42+
function setTopLevelNavigator(navigatorRef) {
43+
_navigator = navigatorRef;
44+
}
45+
46+
function navigate(routeName, params) {
47+
_navigator.dispatch(
48+
NavigationActions.navigate({
49+
type: NavigationActions.NAVIGATE,
50+
routeName,
51+
params,
52+
})
53+
);
54+
}
55+
56+
// add other navigation functions that you need and export them
57+
58+
export default {
59+
navigate,
60+
setTopLevelNavigator,
61+
};
62+
```
63+
64+
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.
65+
66+
```javascript
67+
// any js module
68+
import NavigationService from 'path-to-NavigationService.js';
69+
70+
// ...
71+
72+
NavigationService.navigate('ChatScreen', { userName: 'Lucy' });
73+
```
74+
75+
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 paramaters.

website/sidebars.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@
1919
"status-bar",
2020
"custom-android-back-button-handling",
2121
"connecting-navigation-prop",
22+
"navigating-without-navigation-prop",
2223
"deep-linking",
2324
"screen-tracking",
2425
"redux-integration"

0 commit comments

Comments
 (0)