reactjs - React 对象条目不呈现任何内容
问题描述
所以我有这个代码:
class LatestRecipes extends Component
{
render()
{
return(
<div>
<ul>
{
Object.entries(this.props.latestRecipes).forEach(([key, recipe]) =>
<li key={key}>
<div key={key}>
<Link to={{pathname: '/recipe/' + recipe.Name, state: { recipe: recipe, key: key }}}>{recipe.Name}</Link>
</div>
</li>
)}
</ul>
</div>
);
}
}
但是我的 React App 没有渲染任何li元素。
这是我this.props.latestRecipes
现在的情况:
-LOyADRLntFBWyj1TtGx: {Description: "Description", Ingredients: Array(1), Name: "Name"}
-LOyBKQJcAsR0YfHAjrZ: {Description: "Description", Ingredients: Array(1), Name: "Name"}
我想要的是能够发送key
(-LOyADRLntFBWyj1TtGx)和recipe
配方组件。并在li
元素中渲染配方详细信息
没有错误,没有任何东西,如果我控制台记录它,我会得到我正在寻找的确切结果。
解决方案
你需要 amap
而不是 a forEach
。
该Array.forEach
函数不返回元素数组(它undefined
根据MDN 文档返回),因为它根据map
被映射数组的元素返回一个新数组。
例如:
const timesTwo = [1, 2, 3].map(e => e * 2);
console.log(timesTwo); // output: [2, 4, 6]
同样不适用于forEach
. 你必须这样做:
const timesTwo = [];
[1, 2, 3].forEach(e => {
timesTwo.push(e * 2);
});
console.log(timesTwo); // output: [2, 4, 6]
推荐阅读
- javascript - 模式的正则表达式
- java - 对发布请求进行改造 2 500 内部服务器错误
- document - 如何在 LibreOffice 中将时区添加到时间和日期以包含在页脚的自动更新字段中?
- php - PHP - 如何从数组中对文件所有者进行分组?
- mongodb - 是否有 mongodb 代理或触发器来调用自定义脚本
- jenkins - 从 Slack 触发 jenkins 作业时出错
- pyspark - 从 pyspark 本地机器连接到雪花时出现 Classnotfound 错误
- c# - 在 linux 下构建一个巨大的老式 .net 程序
- c# - 如何在 UWP MediaPlayerElement 中更改信箱/邮筒条的颜色
- html - 在移动设备上如何将导航栏变成汉堡菜单