简体   繁体   English

React Native,元素类型无效:需要一个字符串(对于内置组件)

[英]React Native, element type is invalid: expected a string (for built-in components)

The same issue was already solved in several posts, but non of them helped me (I'm new to React Native, so possibly there is a solution, but I can't find it) 几个帖子已经解决了相同的问题,但是没有一个帮助我(我是React Native的新手,所以可能有解决方案,但我找不到它)

My code uses route and screen. 我的代码使用路线和屏幕。 App.js: App.js:

import React from 'react';
import { StackNavigator } from 'react-navigation';

import HomeScreen from './screens/HomeScreen.js';

const App = StackNavigator({
  Home: { screen: HomeScreen }
});

HomeScreen.js: HomeScreen.js:

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

export default class HomeScreen extends Component {
  onPressLearnMore() {

  }

  render() {
    return (
      <View style={styles.container}>
        <Button title="Learn More" onPress={() => this.onPressLearnMore()} />
      </View>
    );
  }
}

const styles = StyleSheet.create({
  container: {
    flex: 1,
    backgroundColor: '#fff',
    alignItems: 'center',
    justifyContent: 'center',
  },
});

What is wrong? 怎么了?

The project structure: 项目结构:

在此处输入图片说明

您必须在app.js export default App

You don't need to define the file type in your import. 您无需在导入中定义文件类型。

Change this import HomeScreen from './screens/HomeScreen.js'; import HomeScreen from './screens/HomeScreen.js';更改此import HomeScreen from './screens/HomeScreen.js';

To this import HomeScreen from './screens/HomeScreen'; 要从import HomeScreen from './screens/HomeScreen';

Hope it helps. 希望能帮助到你。

Edit: Just to gather all information in this answer, as suggested in the comments you should also export your App.js 编辑:只是为了收集此答案中的所有信息,如评论中所建议,您还应该导出App.js

暂无
暂无

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

相关问题 TypeScript + React:元素类型无效:预期为字符串(对于内置组件) - TypeScript + React: Element type is invalid: expected a string (for built-in components) Expo React Native 错误:元素类型无效:应为字符串(对于内置组件) - Expo React Native Error: Element type is invalid: Expected a string (for built-in components) 元素类型无效:应为字符串(用于内置组件)或类/函数(用于复合组件),但得到:object。 React-Native - Element type is invalid: expected a string (for built-in components) or a class/function (for composite components) but got: object. React-Native 在 React Native App 中,我得到了错误:元素类型无效:期望一个字符串(对于内置组件)...检查 `App` 的渲染方法? - In React Native App, I Got the Error: Element type is invalid: expected a string (for built-in components) ... Check the render method of `App`? 反应错误:元素类型无效:应为字符串(对于内置组件)或类/函数(对于复合组件)但得到:未定义 - React Error: Element type is invalid: expected a string (for built-in components) or a class/function (for composite components) but got: undefined 获取 React 运行时错误:元素类型无效:应为字符串(对于内置组件)或类/函数(对于复合组件) - Getting React Runtime error: Element type is invalid: expected a string (for built-in components) or a class/function (for composite components) 元素类型无效:需要一个字符串(对于内置组件)或一个类/函数(对于复合组件)但得到:未定义的 React - Element type is invalid: expected a string (for built-in components) or a class/function (for composite components) but got: undefined React React - 错误:元素类型无效:需要一个字符串(对于内置组件)或一个类/函数(对于复合组件)但得到:未定义 - React - Error: Element type is invalid: expected a string (for built-in components) or a class/function (for composite components) but got: undefined 错误:元素类型无效:需要一个字符串(对于内置组件)或一个类/函数(对于复合组件)React JS - Error: Element type is invalid: expected a string (for built-in components) or a class/function (for composite components) React JS redux 表单元素类型无效:应为字符串(对于内置组件) - redux form Element type is invalid: expected a string (for built-in components)
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM