问题描述
我创建了一个简单的应用程序,您可以在其中登录,然后使用侧边抽屉加载几个选项卡。
侧面抽屉里有一些按钮,可让您使用深层链接导航到其他页面。
我第一次登录应用程序时,按钮和深层链接工作正常。但是,当我注销并再次登录(登录重新加载选项卡和侧抽屉)时,侧抽屉按钮都不起作用。在我的深层链接处理程序中,我输出了一条控制台消息,并且我意识到深层链接事件没有第二次被触发,这显然是问题所在。
我不知道为什么会发生这种情况,我怀疑这是一个错误。但如果不是这样,我希望有人指出我的代码哪里出了问题以及我应该做什么。
我在下面附加了代码片段。
代码片段
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/