【问题标题】:How to add drawer in react native?如何在反应原生中添加抽屉?
【发布时间】:2024-05-19 21:20:01
【问题描述】:

您好,我有一个简单的应用程序,我想给他添加一个抽屉我使用 react-navigation 4x 并使用 react-navigation-drawer 在我的应用程序中实现抽屉 我在一个单独的包装中单独使用抽屉之前使用它,它工作正常 但是当我使用新包时,我得到了这个错误

Invariant Violation:Invariant Violation:元素类型无效: 期望一个字符串(对于内置组件)或一个类/函数(对于 复合组件)但得到:未定义。您可能忘记导出 你的组件来自它定义的文件,或者你可能混合了 设置默认和命名导入。

虽然我导出了我的屏幕

这里是代码

**navigator.js**

    import React from 'react';
    import {TouchableOpacity, View} from 'react-native';
    import Icon from 'react-native-vector-icons';
    import {createAppContainer, createSwitchNavigator} from 'react-navigation';
    import {
      createDrawerNavigator,
      NavigationDrawerStructure,
    } from 'react-navigation-drawer';
    import {createStackNavigator} from 'react-navigation-stack';
    import ForgetPassword from '../screens/ForgetPassword';
    import Home from '../screens/Home';
    import Splash from '../screens/Splash';

    const AuthStackNavigator = createStackNavigator({
      Authloading: {
        screen: Splash,
        navigationOptions: {
          header: null,
        },
      },
    });

    const HomeStack = createStackNavigator({
      HomeScreen: {
        screen: Home,
        navigationOptions: ({navigation}) => ({
          title: 'Home',
          headerLeft: <NavigationDrawerStructure navigationProps={navigation} />,
          headerRight: (
            <TouchableOpacity
              onPress={() => navigation.navigate('Notifications')}
              style={{margin: 10}}>
              <Icon name="ios-notifications" size={28} color="#1DA1F2" />
            </TouchableOpacity>
          ),
        }),
      },
    });

    const DrawerNavigator = createDrawerNavigator({
      HomeDrawer: {
        screen: HomeStack,
        navigationOptions: {
          drawerLabel: 'Home',
          drawerIcon: () => <Icon name="ios-home" size={28} color="#0496FF" />,
        },
      },
    });

    const Navigations = createSwitchNavigator({
      // Authloading: Splash,
      Auth: AuthStackNavigator, // the Auth stack
      App: DrawerNavigator, // the App stack,
    });

    const Navigator = createAppContainer(Navigations);

    export default Navigator;


**Home.js**

    //import liraries
    import React, {Component} from 'react';
    import {StyleSheet, Text, View} from 'react-native';

    // create a component
    class Home extends Component {
      render() {
        return (
          <View style={styles.container}>
            <Text>Home</Text>
          </View>
        );
      }
    }


    //make this component available to the app
    export default Home;


**App.js**

    import React, {Component} from 'react';
    import Navigator from './src/navigations/navigator';
    class App extends Component {
      render() {
        return <Navigator />;
      }
    }

    export default App;

编辑

我只是认为 NavigationDrawerStructure 它是从 react-native-drawer 导出的,所以这是我的错 :)

所以这里的组件名称为NavigationDrawerStructure

//Navigation Drawer Structure for all screen
class NavigationDrawerStructure extends Component {
  //Structure for the navigatin Drawer
  toggleDrawer = () => {
    //Props to open/close the drawer
    this.props.navigationProps.toggleDrawer();
  };
  render() {
    return (
      <View style={{flexDirection: 'row'}}>
        <TouchableOpacity onPress={this.toggleDrawer}>
          <Icon
            name="ios-menu"
            size={40}
            style={{margin: 10}}
            color="#1DA1F2"
          />
        </TouchableOpacity>
      </View>
    );
  }
}

或者像这样在 Home 堆栈内的 heder 左侧添加一个切换按钮

navigationOptions: ({navigation}) => ({
      title: 'Home',
      headerLeft: (
        <TouchableOpacity onPress={() => navigation.toggleDrawer()}>
          <Icon
            name="ios-menu"
            size={40}
            style={{margin: 10}}
            color="#1DA1F2"
          />
        </TouchableOpacity>
      )
})

【问题讨论】:

  • Route.js?的路径是什么
  • ./src/navigations/Route.js 只是注意其他堆栈的工作方式类似于 AuthStackNavigator 中的 createStackNavigator
  • 这个import Navigator from './src/navigations/navigator'不应该是import Navigator from './src/navigations/Route吗?
  • @Max 是的,这是我的错字,但问题仍然存在 :(
  • 尝试在导入语句中使用 {} 将您的包括起来,例如 import {NewModule} from ../the/path/to/file

标签: javascript android react-native react-native-android react-navigation


【解决方案1】:

导入无效。 react-navigation-drawer中没有这些对象

import { createDrawerNavigator } from 'react-navigation-drawer';
import NavigationDrawerStructure from 'your file path'

改为

import {
      createDrawerNavigator,
      NavigationDrawerStructure,
    } from 'react-navigation-drawer';

【讨论】:

    最近更新 更多