You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

React Navigation:Drawer嵌套Stack下从Profile导航到Feed的方法

在React Navigation嵌套结构中从Profile跳转至Feed的实现方法

先看你提供的导航结构:

function Root() {
  return (
    <Drawer.Navigator>
      <Drawer.Screen name="Home" component={Home} />
      <Drawer.Screen name="Profile" component={Profile} />
      <Stack.Screen name="Settings" component={Settings} />
    </Drawer.Navigator>
  );
}

function App() {
  return (
    <NavigationContainer>
      <Stack.Navigator>
        <Stack.Screen
          name="Root"
          component={Root}
          options={{ headerShown: false }}
        />
        <Stack.Screen name="Feed" component={Feed} />
      </Stack.Navigator>
    </NavigationContainer>
  );
}

这里Feed是外层Stack里和Root同级的屏幕,而Profile属于Root内部的Drawer导航器。因为Profile的导航上下文绑定的是内层Drawer,直接用navigation.navigate('Feed')会找不到目标,得借助父级导航器来实现跳转。

两种可行的实现方式:

  1. 获取父级导航器实例跳转
    在Profile组件里,通过getParent()拿到外层Stack的导航对象,再调用跳转方法:
navigation.getParent().navigate('Feed');
  1. 直接使用绝对路径跳转
    如果内层导航器没有和Feed同名的屏幕,直接调用navigate('Feed')会自动向上查找父级导航器的目标屏幕,部分React Navigation版本支持这种写法:
navigation.navigate('Feed');

内容的提问来源于stack exchange,提问作者Mickael Zana

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.07.31 02:57:31