不变违规:元素类型无效:预期为字符串(用于内置组件)或类/函数,但得到:未定义

时间:2020-06-15 15:34:13

标签: reactjs react-native

我收到此错误:

不变违规:元素类型无效:预期为字符串(对于内置组件)或类/函数,但得到:未定义

我看过其他解决方案,但没有一个能够解决问题

这是我的代码

      import React, {
      Component,
      useState,
      useEffect,
      ActivityIndicator,
    } from "react";
    import { View, Text, StyleSheet } from "react-native";
    import { ScreenContainer } from "react-native-screens";

    export const Home = () => {
      const [isLoading, setisLoading] = useState(true);
      const [dataSource, setdataSource] = useState(null);

      useEffect(() => {
        return fetch("https://facebook.github.io/react-native/movies.json")
          .then((response) => response.json())
          .then((responseJson) => {
            setisLoading(false), setdataSource(responseJson.movies);
          });

      });
      if (isLoading) {
        return (
          <View>
            <ActivityIndicator />
          </View>
        );
      } else {
        let moviest = dataSource.map((val, key) => {
          return (
            <View key={key}>
              <Text>{val.title}</Text>
            </View>
          );
        });

        return (
          <ScreenContainer style={styles.container}>
            <View>
              <Text>These are the movies</Text>
              {moviest} 
              {/* <Apiusers2 /> */}
            </View>
          </ScreenContainer>
        );
      }
    };

1 个答案:

答案 0 :(得分:2)

ActivityIndicatorreact-native而不是react的导出。

更改为此:

import React, {
  Component,
  useState,
  useEffect,
} from "react";
import { View, Text, StyleSheet, ActivityIndicator } from "react-native";
import { ScreenContainer } from "react-native-screens";

将来,此错误几乎总是由错误导入导致的。有时将 default named 导入混用,有时将导入的大小写错误(例如,以“ Activityindicator”代替“ ActivityIndi​​cator”),或者有时是错误的相对路径或节点模块(在这种情况下)。

当您收到此错误时,检查每个导入应该是调试的第一步。