【问题标题】:Expo React Native Drawer Navigator Logout functionalityExpo React Native Drawer Navigator 注销功能
【发布时间】:2020-08-25 09:32:24
【问题描述】:

StackOverflow 我对本机反应非常陌生,因为我现在实现了抽屉导航,现在我想在抽屉的末尾包含一个注销按钮,但我没有找到如何做到这一点的任何好的做法和关于如何实现这一点的想法一种功能。这是我的抽屉代码,我从几个小时的谷歌中找到它,它工作正常,但它具有屏幕功能如果此代码不正确,我找不到如何在此代码中创建注销链接的任何选项,然后建议任何其他好的sn-p提前谢谢

import React from 'react';
import { Ionicons } from '@expo/vector-icons'
import { StyleSheet, Text, View, TouchableOpacity } from 'react-native';
import { createDrawerNavigator, createStackNavigator, createAppContainer } from 'react-navigation';
import { DrawerActions } from 'react-navigation-drawer';

// _signOutAsync = async () => {
//   await AsyncStorage.clear();
//   this.props.navigation.navigate('Auth');
// };
const HomeScreen = () => (
  <View style={styles.container}>
      <Text>Home Screen!</Text>
  </View>
);


const ProfileScreen = () => (
  <View style={styles.container}>
      <Text>Profile Screen!</Text>
  </View>
);


const SettingsScreen = () => (
  <View style={styles.container}>
      <Text>Settings Screen!</Text>
  </View>
);

const DrawerNavigator = createDrawerNavigator({
  Home: {
    screen: HomeScreen, 
    navigationOptions: ({ navigation }) => ({
      title: 'Home Screen', 
      drawerLabel: 'Home',
      drawerIcon: () => (
        <Ionicons name="ios-home" size={20} />
      )
    })
  },
  Profile: {
    screen: ProfileScreen, 
    navigationOptions: ({ navigation }) => ({
      title: 'Profile Screen', 
      drawerLabel: 'Profile',
      drawerIcon: () => (
        <Ionicons name="ios-person" size={20} />
      )
    })
  },
  Settings: {
    screen: SettingsScreen, 
    navigationOptions: ({ navigation }) => ({
      drawerIcon: () => (
        <Ionicons name="ios-settings" size={20} />
      )
    })
  },
 
});


const StackNavigator = createStackNavigator({
  DrawerNavigator: {
    screen: DrawerNavigator, 
    navigationOptions: ({ navigation }) => {
      const { state } = navigation;

      if(state.isDrawerOpen) {
        return {
          headerLeft: ({titleStyle}) => (
            <TouchableOpacity onPress={() => {navigation.dispatch(DrawerActions.toggleDrawer())}}>
              <Ionicons name="ios-close" style={styles.menuClose} size={36} color={titleStyle} />
            </TouchableOpacity>
          ),
         
        }
      }
      else {
        return {
          headerLeft: ({titleStyle}) => (
            <TouchableOpacity onPress={() => {navigation.dispatch(DrawerActions.toggleDrawer())}}>
              <Ionicons name="ios-menu" style={styles.menuOpen} size={32} color={titleStyle} />
            </TouchableOpacity>
          
          ),
          
        }
        
      }
      
    }
  }
})

export default  createAppContainer(StackNavigator);

【问题讨论】:

  • 您可以创建一个注销组件,您可以在其中编写代码以通过从异步存储中清除您的身份验证令牌来导航到登录屏幕或初始屏幕。

标签: javascript react-native expo


【解决方案1】:

您可以创建一个内容组件以在 Drawer Navigator 中呈现,使其更易于修改。 我会用你的例子来解释(我假设它是你的 App.js 文件):

//import CustomDrawer from '...'
const DrawerNavigator = createDrawerNavigator({
  Home: {
    screen: HomeScreen, 
    navigationOptions: ({ navigation }) => ({
      title: 'Home Screen', 
      drawerLabel: 'Home',
      drawerIcon: () => (
        <Ionicons name="ios-home" size={20} />
      )
    })
  },
  Profile: {
    screen: ProfileScreen, 
    navigationOptions: ({ navigation }) => ({
      title: 'Profile Screen', 
      drawerLabel: 'Profile',
      drawerIcon: () => (
        <Ionicons name="ios-person" size={20} />
      )
    })
  },
  Settings: {
    screen: SettingsScreen, 
    navigationOptions: ({ navigation }) => ({
      drawerIcon: () => (
        <Ionicons name="ios-settings" size={20} />
      )
    })
  }, 
 //Here you will add one more pair of curly brackets to add more configs.
}, {
   initialRouteName: 'Home',
   contentComponent: CustomDrawer //This is the option that will allow you to add the button
}); 

您将创建一个完整的组件以根据需要进行修改,然后在 Drawer Navigator 中进行渲染。请记住,我使用的是 CustomDrawer 名称,您将在 App.js 文件中导入此组件。

import React from 'react';
import { View, Text, Image, StyleSheet } from 'react-native';
import { Button } from 'react-native-elements';
import { DrawerNavigatorItems } from 'react-navigation-drawer';

const CustomDrawer = ({ ...props }) => {
    return (
        <>
        <View>
           <DrawerNavigatorItems
              {...props}
              itemsContainerStyle={{}}
              itemStyle={{}}
                 />
        </View>
        <View
            style={{
                flexDirection: 'row',
                alignSelf: 'center',
                position: 'relative',
                marginBottom: 20,
            }}
        >
            <Button
                title={'Log out'}
                buttonStyle={{ width: 200, borderRadius: 20 }}
                onPress={}
            />
        </View>
        </>
    );
};

const styles = StyleSheet.create({});

export default CustomDrawer;

这里我只渲染 CustomDrawer 道具,即您在 App.js 中创建并渲染它的项目(特别是我在 DrawerNavigationItems 中传递的 ...props,因此您可以像您一样自定义它想要像一个普通屏幕一样放置按钮、创建视图并对其应用样式。

你也可以代替创建一个新的屏幕来在你的抽屉导航器中渲染它在你的 App.js 中,但我个人觉得它很混乱

您可以通过this 教程了解更多信息

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-01-04
    • 1970-01-01
    • 2021-01-27
    相关资源
    最近更新 更多