Search code examples
react-navigationreact-navigation-stack

Is there a way to see if a Screen/Route exists in a navigation stack for React Navigation v5?


Depending on if a screen exists in a certain stack I need to be able to do different things in my code. I have tried my best but I haven't found anything on the matter. Is there an easy way to achieve this?


Solution

  • If you mean, you want to get all the routes inside the current navigator, then I think this will fix your problem.

    const navigation = useNavigation();
    
    const routes = navigation.dangerouslyGetState().routeNames;