如何在反应导航中获取当前路线名称?

Esh*_*ist 17 android ios react-native react-navigation

我想在react-navigation中使用当前路线或屏幕的名称,如果条件需要进行一些更改,我想在其中使用它。

小智 76

对于反应导航 v5:

import {useRoute} from '@react-navigation/native';

const route = useRoute();
console.log(route.name);
Run Code Online (Sandbox Code Playgroud)

  • 这很好,但不适用于嵌套路由。 (4认同)
  • 您没有指定我应该在哪个地方使用这个 useRoute() 方法。我发现 Hooks 只能在函数组件的主体内部调用。 (3认同)

Hus*_*urd 26

您可以将其捕获为以下代码:

this.props.navigation.state.routeName
Run Code Online (Sandbox Code Playgroud)

  • 你能检查一下吗,请!/sf/ask/3871470291/ (2认同)

gia*_*ise 16

如果您使用的是嵌套导航器,则可以使用此代码获取当前活动屏幕的状态

const getActiveRouteState = function (route: NavigationState): NavigationState {
    if (!route.routes || route.routes.length === 0 || route.index >= route.routes.length) {
        return route;
    }

    const childActiveRoute = route.routes[route.index] as NavigationState;
    return getActiveRouteState(childActiveRoute);
}
Run Code Online (Sandbox Code Playgroud)

用法:

const activeRoute = getActiveRouteState(this.props.navigation.state);
Run Code Online (Sandbox Code Playgroud)

当我需要从NavigationDrawer获取当前活动屏幕的状态时,可以使用此功能。

  • 我认为这个答案更通用 (2认同)

ooo*_*ala 16

  import {getFocusedRouteNameFromRoute,useRoute} from '@react-navigation/native';

  //...
  const route = useRoute();
  const routeName = getFocusedRouteNameFromRoute(route); // Get Nested Route Name
Run Code Online (Sandbox Code Playgroud)


Sub*_*hra 15

这在 react-navigation v5.x 中工作正常

this.props.route.name
Run Code Online (Sandbox Code Playgroud)


Ris*_*abh 10

在使用"react-navigation": "^3.0.8"和DrawerNavigator时,可以使用this.props对象访问它 this.props.activeItemKey


Kun*_*Luo 10

准备

注册NavigationService.js,在没有导航道具的情况下查看导航中的文档详细信息

<App
  ref={navigatorRef => {
        NavigationService.setTopLevelNavigator(navigatorRef);
  }}
/>
Run Code Online (Sandbox Code Playgroud)

递归函数

function getCurrentRoute(nav){
    if(Array.isArray(nav.routes)&&nav.routes.length>0){
        return getCurrentRoute(nav.routes[nav.index])
    }else {
        return nav.routeName
    }
}
Run Code Online (Sandbox Code Playgroud)

获取当前路线名称

getCurrentRoute(NavigationService.getNavigator().state.nav)
Run Code Online (Sandbox Code Playgroud)


Jus*_*hew 9

可以从附加到导航容器的 navigationRef 获取此信息。哪里navigationRef有参考。

export const navigationRef = React.createRef()

<NavigationContainer
   ref={navigationRef} 
   >
  <Navigator />
</NavigationContainer>
Run Code Online (Sandbox Code Playgroud)

然后使用: const currentRouteName = navigationRef.current.getCurrentRoute().name

或者在功能组件中您可以使用Refconst navigationRef = React.useRef()


use*_*025 8

对于react-navigation v5,您可以使用 useNavigationState 挂钩 -

import {useNavigationState} from '@react-navigation/native';

const state = useNavigationState(state => state);
const routeName = (state.routeNames[state.index]);
console.log(routeName);
Run Code Online (Sandbox Code Playgroud)


小智 7

const routeNameRef = React.createRef();

<NavigationContainer
  ref={navigationRef}
  onReady={() => routeNameRef.current = navigationRef.current.getCurrentRoute().name}
  onStateChange={() => {
    const previousRouteName = routeNameRef.current
    const currentRouteName = navigationRef.current.getCurrentRoute().name

    if (previousRouteName !== currentRouteName) {
      // Do something here with it
    }

    // Save the current route name for later comparision
    routeNameRef.current = currentRouteName
  }}
  >
  {/* ... */}
</NavigationContainer>
  );

export function getCurrentRouteName(action) {
  return routeNameRef;
}
Run Code Online (Sandbox Code Playgroud)

您可以导入函数 getCurrentRouteName 并使用它来获取当前路由名称及其在 React Navigation 5 中的任何嵌套导航器中的工作。


Mar*_*eja 6

对于 5.x 版本,目前最好的方法是getFocusedRouteNameFromRoute

import { getFocusedRouteNameFromRoute } from '@react-navigation/native';

export default function Stack(route) {
  // If the focused route is not found, we need to assume it's the initial screen
  // This can happen during if there hasn't been any navigation inside the screen
  // In our case, it's "Feed" as that's the first screen inside the navigator
  const routeName = getFocusedRouteNameFromRoute(route) ?? 'Feed';
  return <> ..... </>
}
Run Code Online (Sandbox Code Playgroud)


小智 5

在 React Navigation v5 中,我能够使用以下方法提取当前路线名称:

import { useNavigationState } from '@react-navigation/native'    

const routes = useNavigationState(state => state.routes)
const currentRoute = routes[routes.length -1].name
console.log('currentRoute: ',currentRoute)
Run Code Online (Sandbox Code Playgroud)


小智 5

import { useNavigation } from '@react-navigation/native';

const App = () => {
  const navigation = useNavigation();
  const { dangerouslyGetState } = useNavigation();
  const { index, routes } = dangerouslyGetState()
  console.log(routes[index].name);

  return(
    <>
    </>
  )
};
Run Code Online (Sandbox Code Playgroud)