I want the name of the current route or screen in react-navigation which I want to use inside if condition to make some changes.
-
4React is such a mess with all the changes between versions. The docs don't mention any of these & the code seems hard to figure out, I wonder how people even came up with these (old) solutions. – Kevin Apr 20 '20 at 03:56
-
any way for 'wix/react-native-navigation', to get the current screen? – Vandit Mehta Dec 18 '20 at 10:48
35 Answers
For react-navigation v5:
import { useRoute } from '@react-navigation/native';
const route = useRoute();
console.log(route.name);

- 18,734
- 5
- 61
- 83

- 1,862
- 2
- 11
- 7
-
5You did not specify in which place should i use this useRoute() method. I am getting Hooks can only be called inside of the body of a function component. – Aleksandrs Jan 03 '21 at 12:02
-
How to identify this scenario? Navigation directly to Contact screen. And Navigation to Contact screen through Home Screen. – M.Kalidass Apr 13 '21 at 10:33
-
3
-
It will also trigger rerenders on every route change. Just be aware – David Novák May 26 '23 at 17:45
You can catch it as the following code:
this.props.navigation.state.routeName

- 8,066
- 2
- 43
- 38
If you are using nested navigators, you can use this code to get current active screen's state
import { NavigationState } from 'react-navigation';
const getActiveRouteState = function (route: NavigationState): NavigationState {
if (!route.routes || route.routes.length === 0 || route.index >= route.routes.length) {
return route;
}
const childActiveRoute = route.routes[route.index] as NavigationState;
return getActiveRouteState(childActiveRoute);
}
Usage:
const activeRoute = getActiveRouteState(this.props.navigation.state);
I'm using this when I need to get current active screen's state from NavigationDrawer.

- 1,678
- 20
- 32
This works fine in react-navigation v5.x
this.props.route.name

- 1,301
- 6
- 13

- 317
- 2
- 8
const routeNameRef = React.createRef();
<NavigationContainer
ref={navigationRef}
onReady={() => routeNameRef.current = navigationRef.current.getCurrentRoute().name}
onStateChange={() => {
const previousRouteName = routeNameRef.current
const currentRouteName = navigationRef.current.getCurrentRoute().name
if (previousRouteName !== currentRouteName) {
// Do something here with it
}
// Save the current route name for later comparision
routeNameRef.current = currentRouteName
}}
>
{/* ... */}
</NavigationContainer>
);
export function getCurrentRouteName(action) {
return routeNameRef;
}
You can import the function getCurrentRouteName and use this to get the current route name and its working in any nested navigators in React Navigation 5.

- 2,194
- 1
- 18
- 19

- 191
- 1
- 4
-
1Beware of this [gotcha](https://reactnavigation.org/docs/navigating-without-navigation-prop/#handling-initialization) – Anand Rockzz Mar 03 '21 at 19:37
While using "react-navigation": "^3.0.8"
and DrawerNavigator it can be accessed from the this.props object using
this.props.activeItemKey

- 3,752
- 4
- 47
- 74
-
-
1If you are using nested navigators, for example a StackNavigator inside a DrawerNavigator and need the `activeItemKey` say inside the Drawers `contentComponent`, how would you get it? – cseelus Apr 20 '19 at 17:44
-
for a single route this works for me, this.props.navigation.state.routeName – JHOAN B. HENRICHE Nov 26 '19 at 21:06
import {getFocusedRouteNameFromRoute,useRoute} from '@react-navigation/native';
//...
const route = useRoute();
const routeName = getFocusedRouteNameFromRoute(route); // Get Nested Route Name

- 1,485
- 2
- 17
- 21
Preparation
register NavigationService.js
,see the doc detail in Navigating without the navigation prop
<App
ref={navigatorRef => {
NavigationService.setTopLevelNavigator(navigatorRef);
}}
/>
recursion function
function getCurrentRoute(nav){
if(Array.isArray(nav.routes)&&nav.routes.length>0){
return getCurrentRoute(nav.routes[nav.index])
}else {
return nav.routeName
}
}
get current routeName
getCurrentRoute(NavigationService.getNavigator().state.nav)

- 143
- 1
- 7
In React Navigation v5, I was able to pull the current route name with the below approach:
import { useNavigationState } from '@react-navigation/native'
const routes = useNavigationState(state => state.routes)
const currentRoute = routes[routes.length -1].name
console.log('currentRoute: ',currentRoute)

- 137
- 1
- 5
-
Before using this, read the docs about it: https://reactnavigation.org/docs/use-navigation-state/#how-is-usenavigationstate-different-from-navigationgetstate -> it's probably better to use navigation.getState() – Wu Wei Jan 30 '23 at 19:33
It is possible to get this from the navigationRef attached to the navigation container. Where navigationRef
is a ref.
export const navigationRef = React.createRef()
<NavigationContainer
ref={navigationRef}
>
<Navigator />
</NavigationContainer>
Then use: const currentRouteName = navigationRef.current.getCurrentRoute().name
Alternatively in a functional component you can useRef const navigationRef = React.useRef()

- 413
- 5
- 12
For react-navigation v5, you could use the useNavigationState hook -
import {useNavigationState} from '@react-navigation/native';
const state = useNavigationState(state => state);
const routeName = (state.routeNames[state.index]);
console.log(routeName);

- 997
- 13
- 20
With version 5.x the best way currently is getFocusedRouteNameFromRoute
import { getFocusedRouteNameFromRoute } from '@react-navigation/native';
export default function Stack(route) {
// If the focused route is not found, we need to assume it's the initial screen
// This can happen during if there hasn't been any navigation inside the screen
// In our case, it's "Feed" as that's the first screen inside the navigator
const routeName = getFocusedRouteNameFromRoute(route) ?? 'Feed';
return <> ..... </>
}

- 848
- 7
- 10
import { useNavigation } from '@react-navigation/native';
const App = () => {
const navigation = useNavigation();
const { dangerouslyGetState } = useNavigation();
const { index, routes } = dangerouslyGetState()
console.log(routes[index].name);
return(
<>
</>
)
};

- 201
- 3
- 6
You can use this in hooks as well.
console.log(navigation.dangerouslyGetState());

- 722
- 1
- 8
- 18
This is step by step procedure of what Justin.Mathew has described in his answer.
Create a new file called RootNavigation.js
and put the below content inside.
// RootNavigation.js
import * as React from 'react';
export const navigationRef = React.createRef(); // we will access all navigation props by importing this in any of the component
Now import the navigationRef
from the RootNavigation.js
file, and assign NavigationContainer
ref to this. After this step navigationRef
can function as navigation prop globally.
// App.js
import { NavigationContainer } from '@react-navigation/native';
import { navigationRef } from './RootNavigation';
export default function App() {
handleNavigationRef = (ref) => {
// DON'T DO navigationRef = ref, cause this will give you "navigationRef is
// read only" error.
navigationRef.current = ref;
}
return (
<NavigationContainer ref={handleNavigationRef}>
{/* ... */}
</NavigationContainer>
);
}
USAGE
Now you can import navigationRef
in any of the file, even nested ones. And can use this to get the currentRoute and screen details.
//SomeNestedComonent.js
import { navigationRef } from "path/to/RootNavigation.js";
const route = navigationRef.current?.getCurrentRoute(); //current route object
const currentScreen = route.name; // current screen name

- 76
- 2
- 4
In one line with useNavigationState Hook:
const screenName = useNavigationState((state) => state.routes[state.index].name)

- 3,528
- 1
- 33
- 23
If you are using React Navigation v6 you can use this:
import { useRoute } from '@react-navigation/native';
...
const route = useRoute();
console.log('Current Route: ', route.name);
And if you want to get the name of the screen that you are, and you are inside a nested navigator, you can do this:
import { useNavigationState } from '@react-navigation/native';
...
const routes = useNavigationState(state => state.routes);
const currentRouteIndex =
routes?.length && routes[routes.length - 1].state?.index;
const currentRoute =
routes[routes.length - 1].state?.routeNames[currentRouteIndex];
console.log('Current Route: ', currentRoute);

- 195
- 7
this.props.navigation.state.routeName works only in react-navigation 4 but react-navigation 5 doesn't support it.
The current route name can be achieved by using redux:
-Navigator component passes route object as a prop to the child component
-The Child component receives props and could find the route name in route.name
-To get updated route name on the screen change you can use focus event listener on navigation
<====== Parent Component where navigation is implemented ======>
import React from "react";
import { createMaterialTopTabNavigator } from "@react-navigation/material-top-
tabs";
import ChildScreen from "../screens/Home/childScreen";
const Tab = createMaterialTopTabNavigator();
const ParentNavigations = () => {
return (
<Tab.Navigator
>
<Tab.Screen name="ChildScreen" component={ChildScreen} />
</Tab.Navigator>
);
};
export default ParentNavigations;
<===== Child component =====>
import React, { useEffect } from "react";
import { View, StyleSheet } from "react-native";
import { useDispatch } from "react-redux";
import ActionTypes from "../../store/actions/ActionsTypes";
const ChildScreen = ({ navigation, route }) => {
const dispatch = useDispatch();
useEffect(() => {
const unsubscribe = navigation.addListener("focus", () => {
dispatch({ type: ActionTypes.SETROUTE, payload: route.name }); // every time when screen gets focued it will update the route through redux
});
return unsubscribe;
}, [navigation, route]);
return (
<View style={styles.container}>
<Text>Hello</Text>
</View>
);
};
const styles = StyleSheet.create({
container: {
flex: 1,
backgroundColor: "#0C0B0B",
},
});
export default ChildScreen;

- 31
- 4
If you just want to see if the current screen is focused, you can use navigation.isFocused(). https://reactnavigation.org/docs/navigation-prop/#isfocused
Example:
_backAction() {
const { navigation } = this.props;
if (navigation.isFocused()) {
this.setState({
isLeavingApp: true,
});
}
}

- 121
- 1
- 4
const Home = ({ navigation, route }) => {
// you will see screen key, name and params
console.log("ROUTE", route);
// rest of your code
};

- 3,637
- 4
- 19
- 26
-
3While this code may answer the question, providing additional context regarding how and/or why it solves the problem would improve the answer's long-term value. – mufazmi May 21 '21 at 18:20
For react native navigation 5.x use :
props.state.routeNames[props.state.index]

- 450
- 6
- 9
This simple code worked for me. Just add this function to your Util.ts/js file and from your component pass the navigation as the object.
export const getCurrentScreenName = (navigation: any) => {
return navigation.getState().routes[navigation.getState().index].name;
};

- 21
- 1
I have multiple TabNavigator
s nested in a BottomTabNavigator
. I get the current route of the TabNavigator
with:
const pathAndParams = props.navigation.router.getPathAndParamsForState(props.navigation.state) || {}
const activePath = pathAndParams.path

- 11
- 1
This worked for me (I did it inside my navigation drawer)!
const getCurrentRoute = nav => {
if (Array.isArray(nav.routes) && nav.routes.length > 0) {
return getCurrentRoute(nav.routes[nav.index]);
} else {
return nav.routeName;
}
};
const currentNavigation = getCurrentRoute(this.props.navigation.state);

- 319
- 2
- 9
Try this,
const appNavigation = useNavigation();
const currentRoute = appNavigation.getCurrentRoute();
This worked for me. Navigation, and its state received as props were unreliable(at least for drawer navigator at root). So I went with this one, which seems to be giving the global navigation state.
Had to use the navigation prop being received in drawer for drawer specific functions like closeDrawer
or openDrawer
.
export function AppDrawer(props) {
// for closeDrawer, openDrawer etc.
const { navigation: drawerNavigation } = props;
// for referencing current route
const appNavigation = useNavigation();
const currentRoute = appNavigation.getCurrentRoute();
// ... rest of the code
}
Reference for both the variable in console -

- 134
- 1
- 5
If you are using reach navigation version 6 you can retrieve screen name by
props.route.name

- 260
- 2
- 11
We have a lot of answer here but it is hard to apply the fix because navigation is NULL.
WHY?
Scenario 1: We are using hooks function like: useRoute, useNavigationState,... but the navigation don't be mounted yet. So it is null and get the Error.
Scenario 2: We are using navigation object in the current screen like HomeScreen
const Home = ({ navigation, route }) => { console.log("ROUTE", route); // rest of your code };
but navigation is NULL in Root app with presence of NavigationContainer
SOLUTION Make sure to checking navigation is not NULL by using onReady() method of React navigation.
const navigationRef = useRef();
const [routeName, setRouteName] = useState('');
return (
<NavigationContainer
ref={navigationRef}
onReady={() => {
const currentRoute = navigationRef.current.getCurrentRoute();
setRouteName(currentRoute.name);
// Do whatever you want with navigation here!.
}}>
...
</NavigationContainer>);
That's it.

- 2,400
- 1
- 18
- 22
if you want get it in the DrawerContent
props.navigation.getState().routes[0].name

- 338
- 2
- 11
this worked for me try this..
const getCurrentRouteName = () => {
let _index = props.state.index;
let _routeName = props.state.routeNames;
return _routeName[_index]
}

- 82
- 3
- 8
For 'wix/react-native-navigation' below is my working solution,
import { Navigation } from 'react-native-navigation';
// Create a variable and set the value from navigation events
let navComponent = null
Navigation.events().registerComponentDidAppearListener(event => navComponent = event)
// navComponent will have the following structure
{"componentId": "Component9", "componentName": "HomeScreen", "componentType": "Component", "passProps": {}}

- 2,572
- 26
- 41
In my case, I needed to get the bottom nav index as well, this was my method
import {useNavigationState} from '@react-navigation/native';
then
const routes = useNavigationState(state => state.routes);
let place = routes[routes.length - 1];
if (place.name === 'YOUR_BOTTOM_NAV_NAME') {
if (place.state.index === 0) {
//you are in the main screen(BOTTOM_NAV : index 0)
} else {
//else navigate to index 0 screen
navigation.navigate('FirstScreen');
}
} else if (place.name === 'Another_Screen') {
navigation.navigate('navigate_to_the_place_you_want');
} else {
//otherwise come to the first screen
navigation.navigate('FirstScreen');
}

- 1,139
- 12
- 14
Using version: "@react-navigation/native": "^6.0.8" and this.props.navigation, I did it like this:
let navegState = this.props.navigation.getState();
if (!!navegState)
console.log('CURRENT SCREEN', navegState.routes[navegState.index].name);
Just use navegState.routes[navegState.index]
if you want the whole object

- 911
- 11
- 4
-
You can destructure navegState like this ```const { routes, index } = this.props.navigation.getState(); const routeName = routes[index].name;``` – bytehala Jul 20 '23 at 21:53
'@react-navigation/stack'@6.0.11
Worked for me but it's not the best approach.
const routes = navigation.getState().routes;
const currentRoute = routes[routes.length] // will be our current route .name for the name
If you want to look for a specific one
navigation.getState().routes.find((route) => route.name === 'foo')

- 988
- 1
- 8
- 24
For React Navigation v4
In my use case, I needed to access the current routeName in App.js outside of the createAppContainer component so I did this...
onMount, set this.navigator to nav from ref like so
<App
ref={(nav) => {
this.navigator = nav;
}}/>
Then when I need the current route, I access it via
this.navigator.state.nav.routes.routeName

- 698
- 9
- 27
Pass this prop to your Stack.Navigator:
screenListeners={{
state: e=>{
function findCurrentRoute(data){
const state = data.state;
const routes = state.routes;
const currentRoute = routes[state.index];
if(currentRoute.state) return findCurrentRoute(currentRoute)
else return currentRoute;
}
console.log("CURRENT ROUTE NAME: ", findCurrentRoute(e.data).name);
}
}}
Here, I have used recursion to find the deeply nested route.

- 1,560
- 10
- 26