繁体   English   中英

如何在React Native中将组件正确导入导航器?

[英]How can I properly import a Component into my Navigator in React Native?

我在我的index.ios文件中的Navigator中使用另一个文件夹中有一个名为EnterName的组件。 当我将EnterName放在同一个文件中时,我没有遇到任何问题,但是当我尝试从另一个文件导入它时,我得到:

Element type is invalid: expected a string (for built-in components 
or a class/function (for composite components) but got: undefined. 
Check the render method of `Navigator`

我尝试了两种不同的导入EnterName组件的方法,但都没有工作:

import {EnterName} from './App/Components/EnterName'; var EnterName = require('./App/Components/EnterName');

下面是一些使用Navigator文本,并尝试从另一个文件夹使用组件EnterName (当在同一文件中声明EnterName时,这是有效的)。

  render() {
    return (
      <Navigator
        initialRoute={{name: 'Name entry', index: 0}}
        renderScene={(route, navigator) =>
            <EnterName
              name={route.name}
              onForward={() => {
                var nextIndex = route.index + 1;
                navigator.push({
                  name: 'Scene ' + nextIndex,
                  index: nextIndex,
                });
              }}
              onBack={() => {
                if (route.index > 0) {
                  navigator.pop();
                }
              }}
            />
        }
      />
    );
  }
}

而且,如果你想看到EnterName文件,它就在这里:

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

class EnterName extends Component {
  render() {
    return (
      <View style={styles.center}>
        <View style={styles.flowRight}>
          <TextInput style={styles.inputName}
            placeholder="Enter your name"
            textAlign="center"
            onChangeText={(text) => this.setState({text})}
            //value={this.state.text}
          />

          <TouchableHighlight style={styles.button}
          underlayColor='#99d9f4'>
          <Text style={styles.buttonText}> Go </Text>
        </TouchableHighlight>
        </View>
      </View>
      )
  }
}
// The stylesheet is here, and then below it I have:
module.export = EnterName;

你能帮我弄清楚如何模块化我的代码吗?

编辑:我刚刚忘记了module.exports末尾的“s”。 看起来像导出默认类_classname extends Component {是要走的路。

您是否在module.export结束时错过了's'。 它应该是module.exports 在这种情况下,导入应该是

import EnterName from './App/Components/EnterName

您也可以使用而不是module.exports

export default class EnterName extends Component

https://developer.mozilla.org/en/docs/web/javascript/reference/statements/import

暂无
暂无

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM