react-navigation
current route
get route name
react native
navigation tutorial

How to get current route name in react-navigation?

Interview Questions practice on Codemia

Over 8,000 real interview questions from top companies, searchable by company and role.

Browse interview questions

Introduction

Getting the current route name in React Navigation is a common requirement for analytics, conditional headers, and feature flags. The tricky part is that nested navigators can hide the active leaf route, so reading only the top-level state may return a parent route instead of the visible screen. A robust approach handles nested stacks/tabs and works from a central navigation container. This article shows practical patterns for React Navigation route-name access and screen tracking.

Core Sections

1. Access route name inside a screen component

If you are inside a screen, use route.name directly:

tsx
function ProfileScreen({ route }) {
  return <Text>Current route: {route.name}</Text>;
}

This is simplest for component-local behavior.

2. Global active route via navigation container ref

For analytics, track changes in NavigationContainer:

tsx
1import { NavigationContainer, createNavigationContainerRef } from '@react-navigation/native';
2
3export const navigationRef = createNavigationContainerRef();
4
5function getActiveRouteName(state: any): string {
6  const route = state.routes[state.index ?? 0];
7  if (route.state) return getActiveRouteName(route.state);
8  return route.name;
9}
10
11<NavigationContainer
12  ref={navigationRef}
13  onStateChange={() => {
14    const state = navigationRef.getRootState();
15    const routeName = state ? getActiveRouteName(state) : 'Unknown';
16    console.log('active_route', routeName);
17  }}
18>
19  {/* navigators */}
20</NavigationContainer>

This reliably resolves nested active routes.

3. Hook-based approach for focused screen logic

For in-screen side effects:

tsx
1import { useRoute, useFocusEffect } from '@react-navigation/native';
2
3function OrdersScreen() {
4  const route = useRoute();
5
6  useFocusEffect(
7    React.useCallback(() => {
8      console.log('focused', route.name);
9      return () => {};
10    }, [route.name])
11  );
12
13  return null;
14}

This runs when the screen becomes active.

4. Distinguish parent vs leaf routes

In nested navigators, parent route (MainTabs) differs from active child (Settings). For analytics and page titles, use leaf route names. For access control or layout rules, parent route names may be intentional. Define this explicitly.

5. TypeScript-safe route typing

Use typed param lists for safer route handling:

tsx
1type RootStackParamList = {
2  Home: undefined;
3  Details: { id: string };
4};

Typed route names reduce runtime mistakes and improve refactoring safety.

6. Production instrumentation

Throttle or deduplicate route tracking events to avoid noisy analytics. Send events only on actual route change and include navigator depth metadata when debugging nested behavior.

Validation and production readiness

A reliable implementation is not complete until it is validated under realistic conditions. Add a minimal but representative test matrix that includes normal inputs, edge cases, and malformed data. For UI-focused topics, include at least one scenario for lifecycle or timing behavior (initial load, state transition, and cleanup) so regressions are detected when framework versions change. For infrastructure and tooling topics, run commands against a disposable environment before applying in production and capture expected outputs in documentation. This reduces ambiguity when teammates reproduce steps later.

Instrumentation is equally important. Add structured logs around the critical path, including input shape, selected branch decisions, and failure reasons. Keep logs concise and machine-parseable so alerts and dashboards can surface patterns quickly. If operations are expensive or remote (network, filesystem, container orchestration), include timeout handling and explicit retry policy with backoff. Silent retries without bounds are a common source of hidden incidents.

Finally, document assumptions and compatibility boundaries near the code or article examples: runtime versions, platform requirements, and known behavior differences across environments. Add a lightweight checklist for rollouts that covers dependency pinning, backup/rollback strategy, and smoke checks after deployment. Teams that treat these steps as part of the baseline implementation, not optional polish, usually see fewer production surprises and faster recovery when issues occur.

Common Pitfalls

  • Reading only top-level navigator state and missing active nested route.
  • Emitting analytics on every state mutation instead of route changes.
  • Confusing parent route names with visible leaf screen names.
  • Using untyped route strings and introducing silent typos.
  • Running tracking code before navigation state is initialized.

Summary

In React Navigation, current route retrieval depends on context. Inside a screen, route.name is enough. For app-wide tracking, recursively resolve the active leaf route from root navigation state. With typed routes and clean instrumentation boundaries, route-name logic remains accurate and maintainable across nested navigators.


Related reading
Free course
Beginner
7 lessons
2 hours
Tackling System Design Interview Problems

A short course that equips you with the skills to approach system design interviews methodically.

Start the free course
Track what you have practised

A free account saves your progress, solutions and study plan across every problem on Codemia.

Interview Questions practice on Codemia

Over 8,000 real interview questions from top companies, searchable by company and role.

Browse interview questions

All Rights Reserved.