javascript - 错误:无法读取未定义 React.JS 的属性“地图”
问题描述
我正在关注 react js 教程,但我一直遇到这个问题
import React from "react";
import NewsCard from "../NewsCard/NewsCard";
const NewsCards = ({ articles }) => {
return (
<div>
{articles.map((article, i) => {
<NewsCard />;
})}
</div>
);
};
export default NewsCards;
解决方案
似乎您的文章没有 [] 的默认值。您可以如下更改。并且你应该在使用map
函数时给出 key 属性。
const NewsCards = ({ articles }) => {
const data = articles ? articles : []
return (
<div>
{data.map((article, i) => {
<NewsCard key={article.id}/>;
})}
</div>
);
};
推荐阅读
- python - 如何删除标题,转置数据并将两个 csv 合并到一个文件中?
- git - 如果删除了远程分支,如何删除远程提交?
- c# - 在 Startup.cs 的 Configure 中获取域名或主机名和端口
- c++ - 一次读取 8 位小端文件并对其执行二进制操作
- java - 将 java Class<> 转换为 kotlin 的 KClass<>
- javascript - Puppeteer 在返回 HTML 之前未触发点击
- slack-api - `bot_id` 和 `bot_user_id` 有什么区别?我怎样才能获得 bot_id?
- awk - 提取每一行并将其从其中一列附加到另一个文件
- python - 如何在子标题下正确显示图像
- ruby-on-rails - 如何访问 Active Record 类以从 lib 文件夹中的模块/类创建记录?