Skip to content
the poor coder

react native get route name

Poor Man
Published
Reading time
2 min read
On this page

React Native Get Route Name: Explained#

When working with React Native, you might have come across the need to get the name of the current route. This can come in handy when you want to perform certain actions based on the current screen or route.

Method 1: Using React Navigation#

If you are using React Navigation for navigation in your React Native app, you can get the name of the current route using the following code:

JavaScript

    import { useRoute } from '@react-navigation/native';
    
    function MyComponent() {
      const route = useRoute();
      const routeName = route.name;
      
      console.log(routeName); // Output: Name of current route
    }
  

The useRoute hook from React Navigation returns the route object for the current screen. You can then get the name of the route using the name property.

Method 2: Using React Native Navigation#

If you are using React Native Navigation for navigation in your app, you can get the name of the current screen using the following code:

JavaScript

    import { Navigation } from 'react-native-navigation';
    
    function MyComponent() {
      const screenName = Navigation.getCurrentScreenName();
      
      console.log(screenName); // Output: Name of current screen
    }
  

The Navigation.getCurrentScreenName() method from React Native Navigation returns the name of the current screen.

Method 3: Using React Router Native#

If you are using React Router Native for navigation in your app, you can get the name of the current route using the useRouteMatch hook as follows:

JavaScript

    import { useRouteMatch } from 'react-router-native';
    
    function MyComponent() {
      const match = useRouteMatch();
      const routeName = match.path;
      
      console.log(routeName); // Output: Name of current route
    }
  

The useRouteMatch hook from React Router Native returns the match object for the current route. You can then get the name of the route using the path property.

These are some of the ways you can get the name of the current route or screen in your React Native app. Choose the method that best suits your needs and use it accordingly.

Keep going

2023

how to sort an array

How to Sort an Array If you have an array and you want to order its elements in a specific way, you need to use a sorting algorithm. There are several sorting algorithms available, but two of the most commonly used are…

javascript2 min read
2023

jquery mobile or desktop

jQuery Mobile or Desktop? As a web developer, I have had some experience with both jQuery Mobile and jQuery Desktop. Both frameworks have their pros and cons, and which one to use really depends on the specific project…

javascript1 min read
2023

node.js folder structure

Node.js Folder Structure If you are new to Node.js, it can be daunting to create a folder structure that will work for your project. The structure you choose will depend on the size and complexity of your project. Here…

javascript1 min read
ESC