React Native NavigationDrawer - How can i launch an alert inside createDrawerNavigation

梦想与她 提交于 2021-02-08 04:09:05

问题


i am trying to trigger a logout (an alert to confirm) from an item click in a navDrawer made with reactnavigation. Do you know an efficient way to do that?

This is the code in the DrawerNavigator file:

export default createDrawerNavigator({
    Home: { screen: Home },
    Dashboard: { screen: Dashboard },
    Logout: { screen: Logout }
} ...

And when trying to call the last element (Logout) i know i need to call a class which extends from Component class, but the question like the next code, in this case as you can see i returned null in the render method, it clears completely the screen, but when trying to click into an alert button it does not give me anything

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;

So after that the question is: Is there a way to make it (display an alert from clicking a navDrawerItem without rendering a screen? Thank you very much for your help


回答1:


I think instead of defining an empty Logout Screen (that returns null) you can define a customised DrawerComponent that can handle things like prompting Alerts and Navigation to different screens.

By doing so your DrawerNavigator would look something similar to this -

export const DrawerNavigator = createDrawerNavigator({
    Home: { screen: Home },
    Dashboard: { screen: Dashboard },

    ...More Screens
  }, {
    drawerOpenRoute: 'DrawerOpen',
    drawerCloseRoute: 'DrawerClose',
    drawerToggleRoute: 'DrawerToggle',
    drawerWidth: 250,
    useNativeAnimations: true,
    contentComponent: DrawerComponent
})

And you can create your DrawerComponent like this -

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>
  }
}

...

Hope it helps.



来源:https://stackoverflow.com/questions/52277500/react-native-navigationdrawer-how-can-i-launch-an-alert-inside-createdrawernav

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!