javascript - Can someone explain to me why one function works and the other don't?
问题描述
So I am working on learning React, I am doing the Road To React book. In the book he used this code
const List = (props) => (
props.list.map(item => (
<div key={item.objectID}>
<span>
<a href={item.url}> {item.title} </a>
</span>
<span>
{item.author}
</span>
<span>{item.num_comments} </span>
<span>{item.points} </span>
</div>
))
)
And I used this code
const List = (props) => {
return props.list.map(item => {
<div key={item.objectID}>
<span>
<a href={item.url}> {item.title} </a>
</span>
<span>
{item.author}
</span>
<span>{item.num_comments} </span>
<span>{item.points} </span>
</div>
})
}
His worked and mine did not work, I been researching and I do not understand why, if someone can please help me understand why.
解决方案
问题是您将大括号更改(
为仅{
在此处
大括号,...list.map(item => {
并且在您的 jsx 代码之前没有返回语句。
因此,您可以将代码更改为...list.map(item => (
或仅return
在 jsx 之前添加语句。
...list.map(item => {
return <div...
推荐阅读
- python - 如何使用爱普生打印条形码
- python - 同步方法之间的通信
- postgresql - PostgreSQL LISTEN/NOTIFY 具有相同有效负载的每个事务的通知数
- javascript - 在 ReactJS 的子组件中将变量传递给处理程序
- r - 从 R 将记录附加到 SQL Server 中的表
- intellij-idea - 如何在 PhpStorm for JSX 上拆分和加入标签?例如:“ ”到“ ”
- python - SciPy Griddata“得到参数‘方法’的多个值”错误
- google-analytics - 如何在 Google Analytics 上更改网站所有者?
- codeigniter - 记录了codeigniter错误:同时针对文件夹中的所有文件“语言文件不包含数据”
- html - css flexbox布局模块中的表格设计