Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to get current route name in react native using react navigation v5

I just want the name of the current route or screen in react-native which I can use in condtions. I am using react class based component.

like image 622
WapShivam Avatar asked Dec 30 '25 12:12

WapShivam


2 Answers

Keep a ref of your navigation container and then call getCurrentRoute().name on the reference:

class App {
  navigationRef = null

  render() {
    <NavigationContainer ref={ref => this.navigationRef = ref}>
       .....
    </NavigationContainer>
  }

  getCurrentRouteName() {
    return this.navigationRef.getCurrentRoute().name
  }
}

Source: https://reactnavigation.org/docs/screen-tracking/ (I just converted the function based component syntax into the equivalent class component one)

like image 144
gbalduzzi Avatar answered Jan 01 '26 10:01

gbalduzzi


There's a name property within the route props.

function ProfileScreen({ route }) {
  return (
    <View>
      <Text>This is the profile screen of the app</Text>
      <Text>{route.name}</Text>
    </View>
  );
}

source: https://reactnavigation.org/docs/route-prop

like image 39
Wen W Avatar answered Jan 01 '26 10:01

Wen W



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!