javascript - 如何修复元素类型无效 : expected a string (for built-in components)?

标签 javascript reactjs react-native

虽然我的进口没问题,但我还是收到了这个错误。

Invariant Violation: Element type is invalid: expected a string (for built-in components) or a class/function (for composite components) but got: undefined. You likely forgot to export your component from the file it's defined in, or you might have mixed up default and named imports. Check the render method of Weather.

我的代码是这样的:
App.js

import React from 'react';
import { Text, View, StyleSheet } from 'react-native';
import Weather from './components/Weather';

export default class App extends React.Component {
  render() {
    return (
      <View style={styles.container}>
        <Weather />
      </View>
    );
  }
}

Weather.js

import React from 'react';
import { Text, View, StyleSheet } from 'react-native';
import { Icon } from 'react-native-vector-icons/MaterialCommunityIcons';

export default Weather = () => (
  <View style={styles.weatherContainer}>
    <View style={styles.headerContainer}>
        <Icon size={48} name="weather-sunny" color={'#fff'} />
        <Text style={styles.tempText}> Temperature </Text>
    </View>
    <View style={styles.bodyContainer}>
        <Text style={styles.title}>Sunny</Text>
        <Text style={styles.subtitle}>Hurts the eyes!</Text>
    </View>
  </View>
);

我检查了以前的答案,尝试了命名导出和默认导出,但仍然有错误。谢谢。

最佳答案

在我用头撞墙几个小时后,我设法调试了,错误与这个导入有关(不是天气导入):

从 'react-native-vector-icons/MaterialCommunityIcons' 导入 { Icon };
这应该是这样的默认导入:
从...导入图标

关于javascript - 如何修复元素类型无效 : expected a string (for built-in components)?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56418128/

相关文章:

javascript - Twitter 如何做 noscript 重定向,有什么意义?

javascript 如何判断一个数字是否是另一个数字的倍数

reactjs - 除非在 '--jsx' 为 "jsx"时提供了 "react-jsx"标志,否则无法使用 JSX

javascript - 如何将未压缩的 svg 文件放置在 React 组件上

javascript - React Navigation 中的过渡背景颜色

javascript - $.ajax 干扰此脚本中的 .hide() 和 .show() Jquery

javascript - 如何在 WooCommerce 订单完成页面中插入 Google Merchant Review JS 代码

javascript - React.js。错误: Invalid value for <path> attribute d

react-native - 错误: Execution failed for task ':app:processDebugManifest'

react-native - 'expo start -c' 实际执行什么命令?