【问题标题】:React-Native navigation with Stack Navigator使用 Stack Navigator 进行 React-Native 导航
【发布时间】:2018-02-24 16:16:09
【问题描述】:

我正在使用结合了选项卡导航器和堆栈导航器的反应导航。我现在如何天气堆栈导航器堆栈为空或包含一些值

  componentWillMount() {
   var self=this; 
   BackHandler.addEventListener('hardwareBackPress', () => {
      console.log("Props",self.props);
      if(stack of stack navigator has data){
        navigate to Screen required
      }else{
        close the app
      }
   })
   componentWillUnmount() {
     var self=this;
     BackHandler.removeEventListener('hardwareBackPress',self.backNav());
   }
   backNav(){
    console.log("Navigating Back");
    return false;
   }

【问题讨论】:

    标签: javascript reactjs react-native react-navigation


    【解决方案1】:

    阅读官方 GitHub 存储库时,我发现了一个要求 here 的问题。

    所以基本上它应该是这样的:

    BackHandler.addEventListener('hardwareBackPress', () => {
        if(this.props.navigation.state.routes[0].index === 0) 
          return false; //Prevent closing the app
    
        navigation.goBack(); 
    
        return true;
    })
    

    顺便说一句,我只是好奇:你为什么使用self 而不是this

    另外,同样重要的是,看看this

    【讨论】:

    • BTW, I'm just curious: why are you using self rather that this? - this 引用在函数中丢失。只有箭头函数保留this。但我认为,因为它在组件中 this 也应该可以工作。
    • 你总是可以手动绑定你的函数,但正如你所说,'this'应该可以工作,因为它是一个组件。成功了吗?
    • @FacundoLaRocca 正如你提到的,如果条件与道具的响应不匹配。请查找附图以供回复
    • @Sau_Patil 你如何在你的 mainRoot 组件中注入导航器?
    • this.props.navigation.navigate("AccountList");
    猜你喜欢
    • 1970-01-01
    • 2017-08-25
    • 2018-07-30
    • 1970-01-01
    • 1970-01-01
    • 2016-01-08
    • 2021-03-03
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多