react-native - react native 导航: Deep link events not being fired when Tabs and Side Drawer are reloaded

标签 react-native deep-linking react-native-navigation deeplink wix-react-native-navigation

问题描述

我创建了一个简单的应用程序,您可以在其中登录,然后使用侧边抽屉加载几个选项卡。

侧面抽屉里有一些按钮,可让您使用深层链接导航到其他页面。

我第一次登录应用程序时,按钮和深层链接工作正常。但是,当我注销并再次登录(登录重新加载选项卡和侧抽屉)时,侧抽屉按钮都不起作用。在我的深层链接处理程序中,我输出了一条控制台消息,并且我意识到深层链接事件没有第二次被触发,这显然是问题所在。

我不知道为什么会发生这种情况,我怀疑这是一个错误。但如果不是这样,我希望有人指出我的代码哪里出了问题以及我应该做什么。

我在下面附加了代码片段。

代码片段

sidedrawer.js

import React, {Component} from 'react';
import { StyleSheet, Text, View, TouchableOpacity } from 'react-native';

import {Navigation} from 'react-native-navigation';

import Icon from 'react-native-vector-icons/FontAwesome';

export default class SidedrawerComponent extends Component {

    constructor(props){
        super(props);
    }

    render(){
        return(
            <View style={styles.sideMenuStyle}>
              <View style={{height: '20%', borderColor: 'black', borderWidth : 1, backgroundColor : 'white'}}>
              </View>
              <TouchableOpacity onPress={this.goToScreen.bind(this, 'consumerSettings')}>
               <View style={{borderColor : 'black', borderWidth : 1, flexDirection : 'row'}}>
                <Icon name="home" size={30} color="black" />
                <Text style={{color : 'black', fontWeight : 'bold', fontSize : 20, marginLeft : 10}}>Settings</Text>
               </View>
              </TouchableOpacity>
              <TouchableOpacity onPress={this.goToScreen.bind(this, 'aboutUs')}>
               <View style={{borderColor : 'black', borderWidth : 1, flexDirection : 'row'}}>
                <Icon name="home" size={30} color="black" />
                <Text style={{color : 'black', fontWeight : 'bold', fontSize : 20, marginLeft : 10}}>About Us</Text>
               </View>
              </TouchableOpacity>
              <TouchableOpacity onPress={this.goToScreen.bind(this, 'termsAndConditions')}>
               <View style={{borderColor : 'black', borderWidth : 1, flexDirection : 'row'}}>
                <Icon name="home" size={30} color="black" />
                <Text style={{color : 'black', fontWeight : 'bold', fontSize : 20, marginLeft : 10}}>Terms and Conditions</Text>
               </View>
              </TouchableOpacity>
              <TouchableOpacity onPress={this.goToScreen.bind(this, 'inbox')}>
               <View style={{borderColor : 'black', borderWidth : 1, flexDirection : 'row'}}>
                <Icon name="home" size={30} color="black" />
                <Text style={{color : 'black', fontWeight : 'bold', fontSize : 20, marginLeft : 10}}>Inbox</Text>
               </View>
              </TouchableOpacity>
              <TouchableOpacity onPress={this.goToScreen.bind(this, 'logout')}>
               <View style={{borderColor : 'black', borderWidth : 1, flexDirection : 'row'}}>
                <Icon name="home" size={30} color="black" />
                <Text style={{color : 'black', fontWeight : 'bold', fontSize : 20, marginLeft : 10}}>Logout</Text>
               </View>
              </TouchableOpacity>
            </View>
        )
    }

  goToScreen = (screen) => {
    const visibleScreenInstanceId = Navigation.getCurrentlyVisibleScreenId();
    visibleScreenInstanceId.then((result)=>{
    this.props.navigator.handleDeepLink({
           link: screen,
           payload: result.screenId // (optional) Extra payload with deep link
        });
    })
   }
}

const styles = StyleSheet.create({
    sideMenuStyle : {
        backgroundColor : 'white',
        height : '100%'
    }
})

选项卡之一中的深层链接处理程序:

navigatorEvent = event => {
      if(event.type==="NavBarButtonPress" && event.id === "DrawerButton"){
        this.props.navigator.toggleDrawer({
            side : 'left',
            animated : true
        })
      }
      if(event.type == 'DeepLink') {
          if(event.link=="aboutUs" && event.payload=='screenInstanceID5'){
            this.props.navigator.push({
              screen : 'ServiceTracker.AboutUsScreenComponent',
              title : 'About Us'
            })
            this.props.navigator.toggleDrawer({
             side : 'left',
             animated : true
            })
          }
          if(event.link=="termsAndConditions" && event.payload=='screenInstanceID5'){
            this.props.navigator.push({
              screen : 'ServiceTracker.TermsAndConditionsScreenComponent',
              title : 'Terms and Conditions'
            })
            this.props.navigator.toggleDrawer({
             side : 'left',
             animated : true
            })
          }
          if(event.link=="profile" && event.payload=='screenInstanceID5'){
            this.props.navigator.push({
              screen : 'ServiceTracker.ServiceProviderProfileScreenComponent',
              title : 'Profile'
            })
            this.props.navigator.toggleDrawer({
             side : 'left',
             animated : true
            })
          }
          if(event.link=="serviceProviderSettings" && event.payload=='screenInstanceID5'){
            this.props.navigator.push({
              screen : 'ServiceTracker.ServiceProviderSettingsScreenComponent',
              title : 'Settings'
            })
            this.props.navigator.toggleDrawer({
             side : 'left',
             animated : true
            })
          }
          /*if(event.link=="dashboard" && event.payload=='screenInstanceID5'){
            LoadTabs();
            this.props.navigator.toggleDrawer({
             side : 'left',
             animated : true
            })
          }*/
          if(event.link=="inbox" && event.payload=='screenInstanceID5'){
            this.props.navigator.push({
              screen : 'ServiceTracker.InboxScreenComponent',
              title : 'Inbox'
            })
            this.props.navigator.toggleDrawer({
             side : 'left',
             animated : true
            })
          }
          if(event.link=="logout" && event.payload=='screenInstanceID5'){
            this.props.navigator.toggleDrawer({
             side : 'left',
             animated : true
            })
            Navigation.startSingleScreenApp({
              screen : {
              screen : "ServiceTracker.LandingScreenComponent",
              title :  "Landing Screen",
              navigatorStyle : {
                navBarHidden : true
              }
             }
           })
          }
      }
    }

环境

React Native 导航版本:1.1.476 react native 版本:0.55.2 平台:Android 设备信息:Galaxy Nexus Simulator、Oreo (API 27)、调试

最佳答案

我发现了问题 - 深层链接事件触发,但问题是 result.screenId 的值每次重新加载选项卡时,每个屏幕都会发生变化。

因此,在我的深度链接处理程序中,我没有静态检查特定 ID,而是检查 event.payload was == this.props.navigator.screenInstanceID 是否存在,即使 ID 发生变化,这两个变量当然也会匹配,这才是最重要的。

希望这对尝试使用 React Native Navigation 创建基于侧抽屉选项卡的应用程序的其他人有用。

关于react-native - react native 导航: Deep link events not being fired when Tabs and Side Drawer are reloaded,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51455188/

相关文章:

ios - 在Xcode 7上解决 native 问题

reactjs - 如何在 React Native 中创建自定义顶部导航栏

react-native - 背面可见性 : hidden workaround for android in react native

ios - React Native - RCTFatalException : Unhandled JS Exception: "version" is a required argument

android - YouTube 嵌入式视频在 Android 上单击“播放”按钮时显示开发人员选项

ios - 从另一个应用程序打开 TestFlight 应用程序并深层链接到特定应用程序

ios - 如何使用 Gmail 模板链接进入移动应用程序

android - 无法查询存储在 Firebase 动态/深层链接中的参数

javascript - React-Native-Navigation:未定义已使用但尚未注册

react-native - 如何在 React Native 中构建登录和注册系统