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

时间:2022-08-22 20:21:52

I have a component named EnterName in another folder I want to use in my Navigator in my index.ios file. When I put EnterName in the same file I get no problems, but when I try and import it from another file I get:

我有一个名为EnterName的组件在另一个文件夹中,我想在索引中的导航器中使用它。ios文件。当我在同一个文件中输入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`

I've tried two different ways of importing the EnterName component, and neither work:

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

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

从“进口{ EnterName }。/ App /组件/ EnterName ';var EnterName =要求(“。/ App /组件/ EnterName”);

Below is some text that uses Navigator and tries to use the component EnterName from another folder (this works when EnterName is declared in the same file).

下面是一些使用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();
                }
              }}
            />
        }
      />
    );
  }
}

And, if you want to see the EnterName file, it's here:

如果你想看到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;

Can you help me figure out how to modularize my code?

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

EDIT: I just forgot the "s" at the end of module.exports. Seems like export default class _classname extends Component { is the way to go.

编辑:我刚刚忘记了模块结尾的“s”。似乎导出默认的类_classname继承组件{是一种方法。

1 个解决方案

#1


21  

Have you missed 's' at the end of module.export. It should be module.exports. In that case the import should be

你们在模末漏掉了's'吗?出口。它应该module.exports。在这种情况下,导入应该是

import EnterName from './App/Components/EnterName

Instead of module.exports you can also use

而不是模块。导出也可以使用

export default class EnterName extends Component

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

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

#1


21  

Have you missed 's' at the end of module.export. It should be module.exports. In that case the import should be

你们在模末漏掉了's'吗?出口。它应该module.exports。在这种情况下,导入应该是

import EnterName from './App/Components/EnterName

Instead of module.exports you can also use

而不是模块。导出也可以使用

export default class EnterName extends Component

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

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