【问题标题】:How to export a class using DrawerNavigation and import it in another class in React-native?如何使用 DrawerNavigation 导出一个类并将其导入 React-native 的另一个类中?
【发布时间】:2019-08-28 20:22:59
【问题描述】:

在我的 React-Native 项目中,我将 App.js 文件作为我的默认类。在这个类中,我使用了 DrawerNavigation。这里我提供了我的 App.js 类的代码-

App.js

import React, { Component } from 'react';
import { View, Image, TouchableOpacity } from 'react-native';
import {
  createDrawerNavigator,
  createStackNavigator,
  createAppContainer,
} from 'react-navigation';

import Screen1 from './pages/Screen1';
import Screen2 from './pages/Screen2';
import Screen3 from './pages/Screen3';

class NavigationDrawerStructure extends Component {
  toggleDrawer = () => {
    this.props.navigationProps.toggleDrawer();
  };
  render() {
    return (
      <View style={{ flexDirection: 'row' }}>
        <TouchableOpacity onPress={this.toggleDrawer.bind(this)}>
          {/*Donute Button Image */}
          <Image
            source={require('./image/drawer.png')}
            style={{ width: 25, height: 25, marginLeft: 5 }}
          />
        </TouchableOpacity>
      </View>
    );
  }
}

const FirstActivity_StackNavigator = createStackNavigator({

  First: {
    screen: Screen1,
    navigationOptions: ({ navigation }) => ({
      title: 'Demo Screen 1',
      headerLeft: <NavigationDrawerStructure navigationProps={navigation} />,
      headerStyle: {
        backgroundColor: '#FF9800',
      },
      headerTintColor: '#fff',
    }),
  },
});


const Screen2_StackNavigator = createStackNavigator({

  Second: {
    screen: Screen2,
    navigationOptions: ({ navigation }) => ({
      title: 'Demo Screen 2',
      headerLeft: <NavigationDrawerStructure navigationProps={navigation} />,
      headerStyle: {
        backgroundColor: '#FF9800',
      },
      headerTintColor: '#fff',
    }),
  },
});

const Screen3_StackNavigator = createStackNavigator({
  Third: {
    screen: Screen3,
    navigationOptions: ({ navigation }) => ({
      title: 'Demo Screen 3',
      headerLeft: <NavigationDrawerStructure navigationProps={navigation} />,
      headerStyle: {
        backgroundColor: '#FF9800',
      },
      headerTintColor: '#fff',
    }),
  },
});

const DrawerNavigatorExample = createDrawerNavigator({

  Screen1: {
    //Title
    screen: FirstActivity_StackNavigator,
    navigationOptions: {
      drawerLabel: 'Demo Screen 1',
    },
  },
  Screen2: {
    //Title
    screen: Screen2_StackNavigator,
    navigationOptions: {
      drawerLabel: 'Demo Screen 2',
    },
  },
  Screen3: {
    //Title
    screen: Screen3_StackNavigator,
    navigationOptions: {
      drawerLabel: 'Demo Screen 3',
    },
  },
});

export default createAppContainer(DrawerNavigatorExample);

现在,问题是我想将另一个类作为我的默认类,我想从该类导入 App.js,然后启动 App.js 类。但是在 App.js 类中我已经有一个导出-

export default createAppContainer(DrawerNavigatorExample);

在 React-native 中它不允许我导出多个模块。 那么,如果我想导出 App.js 文件并在另一个类的视图中使用它,那么我该怎么做呢?

【问题讨论】:

    标签: javascript react-native


    【解决方案1】:

    默认只能导出一个模块。

    您只能使用export

    export const AppRoute = createAppContainer(DrawerNavigatorExample);
    

    导入那个

    import { AppRoute } from 'App.js';
    

    【讨论】:

      【解决方案2】:

      我认为您可以像这样导出默认类:

      import React, { Component } from 'react';
      import { View, Image, TouchableOpacity } from 'react-native';
      import {
        createDrawerNavigator,
        createStackNavigator,
        createAppContainer,
      } from 'react-navigation';
      
      import Screen1 from './pages/Screen1';
      import Screen2 from './pages/Screen2';
      import Screen3 from './pages/Screen3';
      
      class NavigationDrawerStructure extends Component {
        toggleDrawer = () => {
          this.props.navigationProps.toggleDrawer();
        };
        render() {
          return (
            <View style={{ flexDirection: 'row' }}>
              <TouchableOpacity onPress={this.toggleDrawer.bind(this)}>
                {/*Donute Button Image */}
                <Image
                  source={require('./image/drawer.png')}
                  style={{ width: 25, height: 25, marginLeft: 5 }}
                />
              </TouchableOpacity>
            </View>
          );
        }
      }
      
      const FirstActivity_StackNavigator = createStackNavigator({
      
        First: {
          screen: Screen1,
          navigationOptions: ({ navigation }) => ({
            title: 'Demo Screen 1',
            headerLeft: <NavigationDrawerStructure navigationProps={navigation} />,
            headerStyle: {
              backgroundColor: '#FF9800',
            },
            headerTintColor: '#fff',
          }),
        },
      });
      
      
      const Screen2_StackNavigator = createStackNavigator({
      
        Second: {
          screen: Screen2,
          navigationOptions: ({ navigation }) => ({
            title: 'Demo Screen 2',
            headerLeft: <NavigationDrawerStructure navigationProps={navigation} />,
            headerStyle: {
              backgroundColor: '#FF9800',
            },
            headerTintColor: '#fff',
          }),
        },
      });
      
      const Screen3_StackNavigator = createStackNavigator({
        Third: {
          screen: Screen3,
          navigationOptions: ({ navigation }) => ({
            title: 'Demo Screen 3',
            headerLeft: <NavigationDrawerStructure navigationProps={navigation} />,
            headerStyle: {
              backgroundColor: '#FF9800',
            },
            headerTintColor: '#fff',
          }),
        },
      });
      
      export const DrawerNavigatorExample = createAppContainer({
      
        Screen1: {
          //Title
          screen: FirstActivity_StackNavigator,
          navigationOptions: {
            drawerLabel: 'Demo Screen 1',
          },
        },
        Screen2: {
          //Title
          screen: Screen2_StackNavigator,
          navigationOptions: {
            drawerLabel: 'Demo Screen 2',
          },
        },
        Screen3: {
          //Title
          screen: Screen3_StackNavigator,
          navigationOptions: {
            drawerLabel: 'Demo Screen 3',
          },
        },
      });
      
      export default new NavigationDrawerStructure();
      

      然后,您可以像这样导入:

      import { DrawerNavigatorExample }, NavigationDrawerStructure from './App.js';
      

      【讨论】:

      • 不!语法错误。意外的标记。基本上,对逗号不满意。
      猜你喜欢
      • 2019-04-29
      • 2021-12-01
      • 2021-04-16
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2015-10-28
      • 2016-04-05
      • 2015-08-15
      相关资源
      最近更新 更多