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:
import { NavigationContainer } from '@react-navigation/native';
import { createNativeStackNavigator } from '@react-navigation/native-stack';
const Stack = createNativeStackNavigator();
export default function App() {
return (
<NavigationContainer>
<Stack.Navigator>{/* ... */}</Stack.Navigator>
</NavigationContainer>
);
}
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:
import { NavigationContainer, useNavigationContainerRef } from '@react-navigation/native';
function App() {
const navigationRef = useNavigationContainerRef(); // You can also use a regular ref with `React.useRef()`
return (
<View style={{ flex: 1 }}>
<Button onPress={() => navigationRef.navigate('Home')}>
Go home
</Button>
<NavigationContainer ref={navigationRef}>{/* ... */}</NavigationContainer>
</View>
);
}
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:
navigationRef.navigate(name, params);
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:
navigationRef.resetRoot({
index: 0,
routes: [{ name: 'Profile' }],
});
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:
const state = navigationRef.getRootState();
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:
const route = navigationRef.getCurrentRoute();
Note that the returned route
object will be undefined
if there are no navigators currently rendered.