简体   繁体   English

如何使用DrawerNavigation导出类并将其导入React-native中的另一个类?

[英]How to export a class using DrawerNavigation and import it in another class in React-native?

In my React- Native project , I have App.js file as my default class. 在我的React-Native项目中,我将App.js文件作为我的默认类。 In this is class I have used DrawerNavigation. 在这个类我使用了DrawerNavigation。 Here I have provided the code for my App.js class- 在这里,我提供了我的App.js类的代码 -

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);

Now, the problem is I want to make another class as my default class and from that class I want to import App.js and then lauch the App.js class. 现在,问题是我想将另一个类作为我的默认类,并且从该类我想要导入App.js然后启动App.js类。 But in the App.js class I already have one export- 但是在App.js类中我已经有一个导出 -

export default createAppContainer(DrawerNavigatorExample);

And in React-native it doesn't allow me to export multiple modules. 在React-native中,它不允许我导出多个模块。 So, if I want to Export the App.js file and use it inside the View of another class, then how I can do that? 所以,如果我想导出App.js文件并在另一个类的View中使用它,那么我该怎么做呢?

You can only export one module as default. 您只能导出一个模块作为默认值。

you can use export only 你只能使用export

export const AppRoute = createAppContainer(DrawerNavigatorExample);

to import that 导入它

import { AppRoute } from 'App.js';

I think you can export default your class like this: 我认为您可以像这样导出默认类:

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();

Than, you can import like this: 比,你可以像这样导入:

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何从类组件导出函数? 反应本机 - How to export function from class components? REACT-NATIVE 将JSON对象导出到另一个.js,而无需类声明react-native - Export JSON object to another .js without class declaration react-native react-native:如何定义javascript类 - react-native : How to define a javascript class onPress={() =&gt; this.props.navigation.openDrawer} 未在 react-native 中显示抽屉导航 - onPress={() => this.props.navigation.openDrawer} is not showing drawernavigation in react-native 如何使用另一个js类n-native-native的键从const中获取值? - How can i fetch a value from a const using a key from another js class n react-native? React-Native:在另一个类中访问TextField值 - React-Native: Access TextField value in another class 从 React-Native 应用程序中的另一个类访问静态变量? - Access static variable from another class in React-Native app? 如何在React Native中将createStackNavigator导出为类? - How to export createStackNavigator as class in React Native? React-Native如何从另一个类的构造函数中的函数获取数组 - React-Native How to get an array from a function in the constructor of another class 如何在 react-native 中调用另一个纯类中的方法? - How can I call the methods in another pure class in react-native?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM