gpt4 book ai didi

javascript - React native 清除 Stack Navigator 堆栈

转载 作者:行者123 更新时间:2023-12-04 05:13:40 28 4
gpt4 key购买 nike

我有几个屏幕可以一一浏览。屏幕1->屏幕2-屏幕3->屏幕4-首页

我想要的是当我回家时,应该清除以前的导航历史记录,并且后退按钮不应该转到最后一个导航屏幕,即屏幕 4。目前,当我在主屏幕上按下后退按钮时,它会带我回到堆栈中的最后一条路线是 screen4。我使用了下面的代码。它给了我错误,没有定义路由或关键主页。我已经在 Screens 类中定义了它。任何帮助,将不胜感激。

const resetAction = NavigationActions.reset({
index: 0,
actions: [NavigationActions.navigate({ routeName: 'Home' })],
});

onPress={() => this.props.navigation.dispatch(resetAction)}

最佳答案

在 V5 中,您可以使用

this.props.navigation.reset({
index: 0,
routes: [{name: 'Home'}],
});
使用钩子(Hook)
import {useNavigation} from '@react-navigation/native';

const navigation = useNavigation();

navigation.reset({
index: 0,
routes: [{name: 'Events'}],
});

关于javascript - React native 清除 Stack Navigator 堆栈,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46340580/

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