如何修复Element类型无效:预期为字符串(对于内置组件)?

时间:2019-06-02 19:11:51

标签: javascript reactjs react-native

尽管导入正常,但出现此错误。

  

不变违规:元素类型无效:预期为字符串(对于内置组件)或类/函数(对于复合组件),但得到:未定义。您可能忘记了从定义文件中导出组件,或者可能混淆了默认导入和命名导入。检查function ScaleBalancing(strArr) { const w1 = JSON.parse(strArr[0])[0]; const w2 = JSON.parse(strArr[0])[1]; let weights = JSON.parse(strArr[1]); for (let i = 0; i < weights.length; i++) { if (w1 + weights[i] === w2 || w2 + weights[i] === w1) { return '' + weights[i]; // should return 5 and break out of function right? } //if this for loop is omitted the function returns 5 for (let j = i + 1; j < weights.length; j++) { if (w1 + weights[i] + weights[j] === w2 || w2 + weights[i] + weights[j] === w1 || w1 + weights[i] === w2 + weights[j] || w2 + weights[i] === w1 + weights[j]) { return '' + weights[i] + ',' + weights[j]; //this returns 1,6 } } } return 'not possible'; } // keep this function call here console.log(ScaleBalancing(["[6, 1]", "[5, 10, 6, 1]"]));的渲染方法。

我的代码如下:
App.js

Weather

Weather.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>
    );
  }
}

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

1 个答案:

答案 0 :(得分:2)

在我设法调试的墙壁上砸了几个小时之后,错误与该导入(不是天气导入)有关:

import { Icon } from 'react-native-vector-icons/MaterialCommunityIcons';
这应该是默认的导入,如下所示:
import Icon from ...