-
Notifications
You must be signed in to change notification settings - Fork 1.9k
Update NavigationService example for v3 #317
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
78 changes: 78 additions & 0 deletions
78
website/versioned_docs/version-3.x/navigating-without-navigation-prop.md
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,78 @@ | ||
--- | ||
id: version-3.x-navigating-without-navigation-prop | ||
title: Navigating without the navigation prop | ||
sidebar_label: Navigating without the navigation prop | ||
original_id: navigating-without-navigation-prop | ||
--- | ||
|
||
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. | ||
|
||
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. | ||
|
||
```javascript | ||
// App.js | ||
|
||
import { createStackNavigator, createAppContainer } from 'react-navigation'; | ||
import NavigationService from './NavigationService'; | ||
|
||
const TopLevelNavigator = createStackNavigator({ /* ... */ }) | ||
|
||
const AppContainer = createAppContainer(TopLevelNavigator); | ||
|
||
export default class App extends React.Component { | ||
// ... | ||
|
||
render() { | ||
return ( | ||
<AppContainer | ||
ref={navigatorRef => { | ||
NavigationService.setTopLevelNavigator(navigatorRef); | ||
}} | ||
/> | ||
); | ||
} | ||
} | ||
``` | ||
|
||
In the next step, we define `NavigationService` which is a simple module with functions that dispatch user-defined navigation actions. | ||
|
||
```javascript | ||
// NavigationService.js | ||
|
||
import { NavigationActions } from 'react-navigation'; | ||
|
||
let _navigator; | ||
|
||
function setTopLevelNavigator(navigatorRef) { | ||
_navigator = navigatorRef; | ||
} | ||
|
||
function navigate(routeName, params) { | ||
_navigator.dispatch( | ||
NavigationActions.navigate({ | ||
routeName, | ||
params, | ||
}) | ||
); | ||
} | ||
|
||
// add other navigation functions that you need and export them | ||
|
||
export default { | ||
navigate, | ||
setTopLevelNavigator, | ||
}; | ||
``` | ||
|
||
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. | ||
|
||
```javascript | ||
// any js module | ||
import NavigationService from 'path-to-NavigationService.js'; | ||
|
||
// ... | ||
|
||
NavigationService.navigate('ChatScreen', { userName: 'Lucy' }); | ||
``` | ||
|
||
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. |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.