我使用的是react-native-navigation,我有一堆屏幕。当我从屏幕A转到屏幕B时,我不想让用户选择返回到屏幕A,只是向前。我正在尝试Navigation.popTo("screen.B.id"),但是我得到了这个错误:

有什么方法可以做到这一点吗?提前谢谢。
发布于 2019-02-22 23:11:39
React-navigation您可以像这样重置堆栈:
const resetAction = StackActions.reset({
index: 0,
actions: [NavigationActions.navigate({ routeName: 'screenB' })],
});
this.props.navigation.dispatch(resetAction);React-native-navigation
一种解决方法是捕获后面的监听器,如下所示:
import {BackHandler} from 'react-native';
export default class RoomType extends Component {
_didFocusSubscription;
_willBlurSubscription;
constructor(props) {
super(props);
this._didFocusSubscription = props.navigation.addListener('didFocus',payload =>
BackHandler.addEventListener('hardwareBackPress', this.onBackButtonPressAndroid)
);
}
}
componentDidMount() {
this._willBlurSubscription = this.props.navigation.addListener('willBlur', payload =>
BackHandler.removeEventListener('hardwareBackPress', this.onBackButtonPressAndroid)
);
}
componentWillUnmount() {
this._didFocusSubscription && this._didFocusSubscription.remove();
this._willBlurSubscription && this._willBlurSubscription.remove();
}
onBackButtonPressAndroid = () => {
//code when you press the back button
};发布于 2019-02-22 23:44:36
您可以尝试将屏幕B设置为新的根目录。
setStackRoot(componentId, params)
如果有必要,也许你必须使用popToRoot(componentId, mergeOptions?)。
发布于 2019-02-24 05:31:21
在react原生导航中,有两个选项可供选择,以实现我相信您正在寻找的目标。
topBar选项中,特别是在您选择的子组件中。backButton: {
visible: false
}举个小例子,你不想要的put的后退选项:
component: {
id: 'screenB',
name: 'screenB',
options: {
title: {
text: 'screenB'
},
topBar: {
// the other options you want
backButton: {
visible: false
}
}
}
}照我的想法,
选项1.是一种简单的展开方式,只需从特定屏幕上移除后退按钮,即可禁用返回到原始屏幕的功能。
选项2.当你想要从应用程序本身的整个等式中删除前一个屏幕时,这是很好的。
我个人的选择2的用例:我做了一个应用程序,最初打开一个登录/注册堆栈。登录/注册后,我将该信息保存到AsyncStorage,并将根目录完全重置到主页。当第二次打开应用程序时,它会检查AsyncStorage中的用户信息。如果应用程序找到用户信息,它会为主页和应用程序的其余部分设置根目录。如果应用程序没有找到用户信息,它会将根设置为登录/注册堆栈,然后循环继续。
我希望这能帮到你!
https://stackoverflow.com/questions/54829818
复制相似问题