虽然我的进口没问题,但我还是收到了这个错误。
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/