【问题标题】:Element type invalid in React NativeReact Native 中的元素类型无效
【发布时间】:2021-07-29 16:01:48
【问题描述】:

我是本机反应的新手,我正在学习教程。这是我的 App.js 文件: App.js

import React, { useState, useEffect,FlatList } from 'react';
import {View,Text,ImageBackground,Button} from 'react-native';
import styles from './styles';

const ArticleItem = ({article}) => {
  const { description, url, urlToImage } = article;
  return (
    <View>
      <Image source={{ uri: urlToImage }} />
      <Text>
        { title }
      </Text>
      <Text>
        { description }
      </Text>
      <Button onPress = { () => { console.log("Button pressed!")} } title="Open" />
      <Button onPress = { () => { console.log("Button pressed!") } } title="Read later" />
    </View>
  )
}

const SplashScreen = (props) => {
  return (
    <View style = { styles.container } >
    <ImageBackground  style= { styles.backgroundImage } source={{uri: 'http://i.imgur.com/IGlBYaC.jpg'}} >
            
      <View style= { styles.logoContainer }>
        <Text style = { styles.logoText }>
          Newzzz
        </Text>
        <Text style = { styles.logoDescription }>
          Get your doze of daily news!
        </Text>
        
      </View>
      </ImageBackground>
    </View>
  );
} 

const HomeScreen = (props) => {
  console.log("articles: ", props.articles);
  return (
    <View>
        <FlatList
          data={ props.articles }
         // renderItem={({item}) => <ArticleItem article = { item }  />}
          keyExtractor={(item, index) => index.toString()}
          renderItem={({item}) => <ArticleItem article = { item }/>}
        />
    </View>
  );
} 

const App = () => {
  const URL = 'https://raw.githubusercontent.com/nimramubashir/React-Native/fetch/articles.json';
  const [articles, setArticles] = useState([]);
  const [loading, setLoading ] = useState(true);
  useEffect(()=>{
    fetch(URL)
    .then((response) => response.json())
    .then((responseJson) => {
      return responseJson.articles;
    })
    .then( articles  => {
      setArticles(articles);
      console.log(articles);
      setLoading(false);
    })
    .catch( error => {
      console.error(error);
    });
    
  } , []);
  
  if (loading){
      return <SplashScreen />
    } else {
      return <HomeScreen articles = { articles }/>
  }
};

export default App

虽然我认为导入/导出语句没有任何问题,但我得到以下信息。

错误:元素类型无效:应为字符串(对于内置组件)或类/函数(对于复合组件),但得到:未定义。您可能忘记从定义组件的文件中导出组件,或者您可能混淆了默认导入和命名导入。 检查HomeScreen的渲染方法。

此错误发生在我更改我的ArticleItem 组件并添加按钮之后。我正在导入 Button 组件。 HomeScreen 组件有问题吗?我不明白。

【问题讨论】:

    标签: reactjs react-native react-button


    【解决方案1】:

    您尚未导入您在HomeScreen 中渲染的FlatList

    【讨论】:

    • 或者你忘记在你的ArticleItem 组件中导入title
    • 我在代码中导入 FlatlLst 问题依然存在
    • 是的,我也忘记导入图像,这就是它不起作用的原因。 @Auticcat
    猜你喜欢
    • 2018-02-07
    • 1970-01-01
    • 2018-05-18
    • 2017-09-16
    • 2021-01-08
    • 2018-07-12
    • 2018-05-29
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多