NavigationContainer
The NavigationContainer
is responsible for managing your app state and linking your top-level navigator to the app environment.
The container takes care of platform specific integration and provides various useful functionality:
- Deep link integration with the
linking
prop. - Notify state changes for screen tracking, state persistence etc.
- Handle system back button on Android by using the
BackHandler
API from React Native.
Usage:
1 2 3 4 5 6 7 8 9 10 11 12 |
|
Ref¶
It's also possible to attach a ref
to the container to get access to various helper methods, for example, dispatch navigation actions. This should be used in rare cases when you don't have access to the navigation
prop, such as a Redux middleware.
Example:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 |
|
If you're using a regular ref object, keep in mind that the ref may be initially null
in some situations (such as when linking is enabled). To make sure that the ref is initialized, you can use the onReady
callback to get notified when the navigation container finishes mounting.
See the Navigating without the navigation prop guide for more details.
Methods on the ref¶
The ref object includes all of the common navigation methods such as navigate
, goBack
etc. See docs for CommonActions
for more details.
Example:
1 |
|
All of these methods will act as if they were called inside the currently focused screen. It's important note that there must be a navigator rendered to handle these actions.
In addition to these methods, the ref object also includes the following special methods:
resetRoot
¶
The resetRoot
method lets you reset the state of the navigation tree to the specified state object:
1 2 3 4 |
|
Unlike the reset
method, this acts on the root navigator instead of navigator of the currently focused screen.
getRootState
¶
The getRootState
method returns a navigation state object containing the navigation states for all navigators in the navigation tree:
1 |
|
Note that the returned state
object will be undefined
if there are no navigators currently rendered.
getCurrentRoute
¶
The getCurrentRoute
method returns the route object for the currently focused screen in the whole navigation tree:
1 |
|
Note that the returned route
object will be undefined
if there are no navigators currently rendered.
getCurrentOptions
¶
The getCurrentOptions
method returns the options for the currently focused screen in the whole navigation tree:
1 |
|
Note that the returned options
object will be undefined
if there are no navigators currently rendered.
addListener
¶
The addListener
method lets you listen to the following events:
state
¶
The event is triggered whenever the navigation state changes in any navigator in the navigation tree:
1 2 3 4 5 6 7 |
|
This is analogous to the onStateChange
method. The only difference is that the e.data.state
object might contain partial state object unlike the state
argument in onStateChange
which will always contain the full state object.
options
¶
The event is triggered whenever the options change for the currently focused screen in the navigation tree:
1 2 3 4 |
|
Props¶
initialState
¶
Prop that accepts initial state for the navigator. This can be useful for cases such as deep linking, state persistence etc.
Example:
1 2 3 4 5 |
|
Providing a custom initial state object will override the initial state object obtained via linking configuration or from browser's URL. If you're providing an initial state object, make sure that you don't pass it on web and that there's no deep link to handle.
Example:
1 2 3 4 5 |
|
See state persistence guide for more details on how to persist and restore state.
onStateChange
¶
Note: Consider the navigator's state object to be internal and subject to change in a minor release. Avoid using properties from the navigation state object except
index
androutes
, unless you really need it. If there is some functionality you cannot achieve without relying on the structure of the state object, please open an issue.
Function that gets called every time navigation state changes. It receives the new navigation state as the argument.
You can use it to track the focused screen, persist the navigation state etc.
Example:
1 2 3 4 5 |
|
onReady
¶
Function which is called after the navigation container and all its children finish mounting for the first time. You can use it for:
- Making sure that the
ref
is usable. See docs regarding initialization of the ref for more details. - Hiding your native splash screen
Example:
1 2 3 4 5 |
|
onUnhandledAction
¶
Function which is called when a navigation action is not handled by any of the navigators.
By default, React Navigation will show a development-only error message when an action was not handled. You can override the default behavior by providing a custom function.
linking
¶
Configuration for linking integration used for deep linking, URL support in browsers etc.
Example:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 |
|
See configuring links guide for more details on how to configure deep links and URL integration.
Options¶
linking.prefixes
¶
URL prefixes to handle. You can provide multiple prefixes to support custom schemes as well as universal links.
Only URLs matching these prefixes will be handled. The prefix will be stripped from the URL before parsing.
Example:
1 2 3 4 5 6 7 8 9 10 11 12 |
|
This is only supported on iOS and Android.
linking.config
¶
Config to fine-tune how to parse the path. The config object should represent the structure of the navigators in the app.
For example, if we have Catalog
screen inside Home
screen and want it to handle the item/:id
pattern:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 |
|
The options for parsing can be an object or a string:
1 2 3 4 5 |
|
When a string is specified, it's equivalent to providing the path
option.
The path
option is a pattern to match against the path. Any segments starting with :
are recognized as a param with the same name. For example item/42
will be parsed to { name: 'item', params: { id: '42' } }
.
The initialRouteName
option ensures that the route name passed there will be present in the state for the navigator, e.g. for config:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 |
|
and URL : /item/42
, the state will look like this:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 |
|
The parse
option controls how the params are parsed. Here, you can provide the name of the param to parse as a key, and a function which takes the string value for the param and returns a parsed value:
1 2 3 4 5 6 7 8 9 10 |
|
If no custom function is provided for parsing a param, it'll be parsed as a string.
linking.enabled
¶
Optional boolean to enable or disable the linking integration. Defaults to true
if the linking
prop is specified.
linking.getInitialURL
¶
By default, linking integrates with React Native's Linking
API and uses Linking.getInitialURL()
to provide built-in support for deep linking. However, you might also want to handle links from other sources, such as Branch, or push notifications using Firebase etc.
You can provide a custom getInitialURL
function where you can return the link which we should use as the initial URL. The getInitialURL
function should return a string
if there's a URL to handle, otherwise undefined
.
For example, you could do something like following to handle both deep linking and Firebase notifications:
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 |
|
This option is not available on Web.
linking.subscribe
¶
Similar to getInitialURL
, you can provide a custom subscribe
function to handle any incoming links instead of the default deep link handling. The subscribe
function will receive a listener as the argument and you can call it with a URL string whenever there's a new URL to handle. It should return a cleanup function where you can unsubscribe from any event listeners that you have setup.
For example, you could do something like following to handle both deep linking and Firebase notifications:
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 |
|
This option is not available on Web.
linking.getStateFromPath
¶
You can optionally override the way React Navigation parses links to a state object by providing your own implementation.
Example:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 |
|
linking.getPathFromState
¶
You can optionally override the way React Navigation serializes state objects to link by providing your own implementation. This is necessary for proper web support if you have specified getStateFromPath
.
Example:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 |
|
fallback
¶
React Element to use as a fallback while we resolve deep links. Defaults to null
.
If you have a native splash screen, please use onReady
instead of fallback
prop.
documentTitle
¶
By default, React Navigation automatically updates the document title on Web to match the title
option of the focused screen. You can disable it or customize it using this prop. It accepts a configuration object with the following options:
documentTitle.enabled
¶
Whether document title handling should be enabled. Defaults to true
.
documentTitle.formatter
¶
Custom formatter to use if you want to customize the title text. Defaults to:
1 |
|
Example:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 |
|
theme
¶
Custom theme to use for the navigation components such as the header, tab bar etc. See theming guide for more details and usage guide.
independent
¶
Whether this navigation container should be independent of parent containers. If this is not set to true
, this container cannot be nested inside another container. Setting it to true
disconnects any children navigators from parent container.
You probably don't want to set this to true
in a typical React Native app. This is only useful if you have navigation trees that work like their own mini-apps and don't need to navigate to the screens outside of them.