簡體   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