如何将“hardwareBackPress"设置为另一个堆栈 [英] How to set 'hardwareBackPress' to another Stack

查看:49
本文介绍了如何将“hardwareBackPress"设置为另一个堆栈的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

//TermsPage.tsx
const resetAction = StackActions.reset({
  index: 0,
  actions: [NavigationActions.navigate({
    routeName: 'BottomTabNav',
    params:{showTerms:false}
  }),
  ],
});
componentWillUnmount() {
    BackHandler.addEventListener('hardwareBackPress',()=>{
      this.props.navigation.dispatch(resetAction)
      return true
    })
  }

如何设置hardwareBackPress"事件监听器以导航到另一个 StackNavigator.如果我像上面那样设置.这种背压适用于所有页面.我只想为TermsPage 设置这个监听器.并将此侦听器设置为导航到另一个 StackNavigator

How to set 'hardwareBackPress' eventListenner to navigate to another StackNavigator. If I set like above. This backpress work in all Pages. I want to set this listener only for TermsPage. And set this listener to navigate to another StackNavigator

推荐答案

我用函数 pop() 解决了这个问题.在页面中,我想去另一个堆栈,我添加了

I solve this problem with function pop(). In the page, where I want to go another stack I added

const resetAction = StackActions.reset({
  index: 0,
  actions: [NavigationActions.navigate({
    routeName: 'BottomTabNav',
    params:{showTerms:false}
  }),
  ],
});

handleBackButtonClick() {
    this.props.navigation.dispatch(resetAction);
    return true;
  }

在其他页面中

handleBackButtonClick() {
    // @ts-ignore
    this.props.navigation.pop();
    return true;
  }
``` And voila

这篇关于如何将“hardwareBackPress"设置为另一个堆栈的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆