我正在尝试通过使用reactnavigation制作的navDrawer中的项目单击触发注销(确认警报)。您知道一种有效的方法吗?
这是DrawerNavigator文件中的代码:
export default createDrawerNavigator({
Home: { screen: Home },
Dashboard: { screen: Dashboard },
Logout: { screen: 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而不显示屏幕来显示警报?非常感谢您的帮助
答案 0 :(得分:1)
我认为可以定义一个自定义的DrawerComponent
来处理提示 Alerts 和 Navigation 之类的内容,而不是定义一个空的注销屏幕(返回空值)。到不同的屏幕。
这样做,您的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>
}
}
...
希望有帮助。