Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

React Navigation 5, get closest parent navigator name

Imagine you have this navigation system:

  -TabNavigator
      --TabA
          ---StackNavigatorA
             ----ScreenA
             ----StackNavigatorB
      --TabB
          ---StackNavigatorB
             ----ScreenB

Is there any way, using React Navigation 5, to get the closest parent navigator name? For example, if I am in the screen "ScreenA" and execute the method I need, this will return "StackNavigatorA"

Something like this:

     const currentNavigator = navigation.getCurrentNavigator(); // <--- getCurrentNavigator doesn't exist, this is pseudocode.
     console.log(currentNavigator); // Will show the closest parent navigator

Thank you.

like image 300
Victor Molina Avatar asked Nov 03 '20 02:11

Victor Molina


Video Answer


1 Answers

To get closest navigation parent you can use dangerouslyGetParent() from navigation prop.

Also, "This method will return undefined if there is no parent navigator. Be sure to always check for undefined when using this method."

To check if navigator is its parents, you can define a custom key to each navigator to do your validations when necessary.

like image 158
Bruno Cardoso Avatar answered Sep 22 '22 16:09

Bruno Cardoso