我试图通过使用reactnavigation进行的navDrawer中的项目单击来触发注销(警报确认)。你知道一种有效的方法吗?
这是DrawerNavigator文件中的代码:
export default createDrawerNavigator({
Home: { screen: Home },
Dashboard: { screen: Dashboard },
Logout: { screen: Logout }
} ...
当我试图调用最后一个元素(Logout)时,我知道我需要调用一个从Component类扩展的类,但问题就像下一个代码一样,在这种情况下你可以看到我在render方法中返回null,它会清除完全是屏幕,但是当试图点击进入警报按钮时,它不会给我任何东西
import React, {Component} from 'react';
import { Alert } from 'react-native';
class Logout extends Component{
constructor(props){
console.log('those are the props '+JSON.stringify(props))
super(props);
this.logoutAlert();
}
logout = ()=>{
//const deleted_element = clearAllData();
console.log('Logout.js - Element deleted ');
}
canceledLogout = () => {
console.log('The logout process is now cancelled');
}
logoutAlert() {
Alert.alert(
'Confirm',
'Are you sure that you want to logout?',
[
{ text: 'Yes', onPress: () => this.logout },
{ text: 'Cancel', onPress: () => this.canceledLogout }
]
);
}
render(){
return null;
}
}
export default Logout;
所以在那之后问题是:有没有办法实现它(通过单击navDrawerItem显示警报而不渲染屏幕?非常感谢你的帮助
我认为,您可以定义一个自定义的DrawerComponent
,而不是定义一个空的Logout Screen(返回null),它可以处理诸如提醒警报和导航到不同屏幕之类的事情。
这样你的DrawerNavigator
看起来就像这样 -
export const DrawerNavigator = createDrawerNavigator({
Home: { screen: Home },
Dashboard: { screen: Dashboard },
...More Screens
}, {
drawerOpenRoute: 'DrawerOpen',
drawerCloseRoute: 'DrawerClose',
drawerToggleRoute: 'DrawerToggle',
drawerWidth: 250,
useNativeAnimations: true,
contentComponent: DrawerComponent
})
你可以像这样创建你的DrawerComponent
-
class DrawerComponent extends React.Component {
navigateTo = (routeName) => {
this.props.navigation.navigate(routeName)
}
logout = ()=>{
//const deleted_element = clearAllData();
console.log('Logout.js - Element deleted ');
}
canceledLogout = () => {
console.log('The logout process is now cancelled');
}
logoutAlert = () => {
Alert.alert(
'Confirm',
'Are you sure that you want to logout?',
[
{ text: 'Yes', onPress: () => this.logout },
{ text: 'Cancel', onPress: () => this.canceledLogout }
]
);
}
render() {
<ScrollView>
<TouchableOpacity onPress={() => this.navigateTo('Home')}> Home </TouchableOpacity>
<TouchableOpacity onPress={() => this.navigateTo('Dashboard')}> Dashboard </TouchableOpacity>
<TouchableOpacity onPress={this.logoutAlert}> Logout </TouchableOpacity>
</ScrollView>
}
}
...
希望能帮助到你。