gpt4 book ai didi

reactjs - 如何重置嵌套导航器 (react-navigation v5)

转载 作者:行者123 更新时间:2023-12-03 18:36:19 28 4
gpt4 key购买 nike

有两组堆栈导航器;

const SetOneScreens = () => (
<Stack.Navigator initialRouteName="AScreen">
<Stack.Screen name="AScreen" component={AScreen} />
<Stack.Screen name="BScreen" component={BScreen} />
</Stack.Navigator>
);
const SetTwoScreens = () => (
<Stack.Navigator initialRouteName="CScreen">
<Stack.Screen name="CScreen" component={CScreen} />
<Stack.Screen name="DScreen" component={DScreen} />
</Stack.Navigator>
);
哪些嵌套在抽屉导航器中
    <NavigationContainer>
<Drawer.Navigator initialRouteName="SetOneScreens">
<Drawer.Screen name="SetOneScreens" component={SetOneScreens} />
<Drawer.Screen name="SetTwoScreens" component={SetTwoScreens} options={{swipeEnabled: false}} />
</Drawer.Navigator>
</NavigationContainer>
我想从 BScreen 导航至 DScreen并重置堆栈(为了不让android中的硬件后退按钮回到 BScreen )
在嵌套的情况下,我们应该首先定义导航器名称;我应该如何在重置操作中定义屏幕。
// For navigation 
props.navigation.navigate('setTwoScreens',{screen:'DScreen'})

// For reset I can only navigate to initial screen
props.navigation.reset({index:0,routes:[{name:'setTwoScreens'}]})
我应该如何处理 resetnavigationCommonActions

最佳答案

documentation 中所写在 react-navigation-v5 中,您需要使用 reset-action 调度 CommonAction 以清除应用程序的后台堆栈,以便在用户按下设备的硬件后退按钮时应用程序不会返回上一屏幕,请查看以下示例,

import { CommonActions } from "@react-navigation/native";

props.navigation.dispatch({
...CommonActions.reset({
index: 0,
routes: [{ name: "AnotherStackNavigator" }]
})
});
或者,如果您想重置为该 StackNavigator 中的特定屏幕,您可以这样做:
props.navigation.dispatch({
...CommonActions.reset({
index: 0,
routes: [
{
name: "AnotherStackNavigator",
state: {
routes: [
{
name: "AnotherStackNavigatorScreen",
params: {
...
}
}
]
}
}
]
})
});

关于reactjs - 如何重置嵌套导航器 (react-navigation v5),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/63442408/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com