【发布时间】:2021-06-01 21:09:01
【问题描述】:
我有一个复杂的导航器层次结构。但我会专注于这个问题:
Stack navigator
-- Tab navigator
---- Home stack navigator (initial route)
---- Other screens
-- Other screens
用于导航的navigation 对象由屏幕的道具或通过在任何屏幕子组件上使用挂钩useNavigation() 提供。
我在我的应用中使用深度链接来导航几乎所有地方。我的主要根导航器容器是选项卡导航器。因此,将用户从那里路由到其他屏幕/选项卡似乎非常合乎逻辑。我使用了navigation 传递给useEffect 中的选项卡导航器的道具,它工作得很好,这让我感到惊讶,因为我在导航器(而不是屏幕)中使用它。
现在我知道这是什么问题了!当应用程序从退出状态重新加载时,navigation.navigate(...) 在这种情况下不起作用(没有抛出任何东西)。这是因为导航似乎发生在导航器被渲染之前。
我检查了this 是否有可能的解决方案,但它并没有解决我的问题,因为NavigationContainer 已准备就绪,但选项卡导航器没有。
当我在主屏幕上将导航移动到useFocusEffect 时,它工作正常。
如何验证导航器是否已完成渲染?
示例代码:
function MainTabNav({ navigation }) {
useEffect(() => {
console.log("trying to navigate...");
navigation.navigate("other_screen")
}, [navigation]);
return (
<Tab.Navigator
initialRouteName={...}
>
... // Stack navigators for home & other screens
</Tab.Navigator>
);
}
【问题讨论】:
标签: react-native react-navigation react-navigation-v5