reactjs - React Typescript列表映射表行未呈现
问题描述
我有一个称为项目的对象列表。我想将列表中的每个对象放到表中的一行。但是,这些行没有被呈现。我正在使用带有 react-bootstrap 的打字稿。我想知道为什么会发生这种情况以及如何解决它。谢谢!
模板.tsx
export default function Templates() {
const items = [
{
"name": "Item 1",
"alt": "First",
"description": "This is the first item"
},
{
"name": "Item 2",
"alt": "Second",
"description": "This is the second item"
},
{
"name": "Item 3",
"alt": "Third",
"description": "-"
}
]
return (
<>
<MyTable items={items}>MyTable</MyTable>
</>
)
}
我的表.tsx
import React from 'react'
import { Table, Button } from 'react-bootstrap'
type TableProps = {
items: {
name: string,
alt: string,
description: string
}[]
}
const MyTable: React.FunctionComponent<TableProps> = ({
items
}) => {
return (
<>
<Table striped bordered hover variant="dark">
<thead>
<tr>
<th>Name</th>
<th>Alt</th>
<th>Description</th>
</tr>
</thead>
<tbody>
{items.map(item => {
<tr>
<td>{item.name}</td>
<td>{item.alt}</td>
<td>{item.description}</td>
</tr>
})}
</tbody>
</Table>
</>
)
};
export default MyTable;
现在只渲染了thead。
解决方案
您正在映射项目并且映射回调返回未定义(无返回语句),请确保返回迭代项目:
{items.map(item => {
return (
<tr key={item.name}>
<td>{item.name}</td>
<td>{item.alt}</td>
<td>{item.description}</td>
</tr>
)
})}
推荐阅读
- javascript - 如何获得信使中使用的“竖起大拇指”的可点击“id”?
- nlp - 如何回答大文件中的问题?
- c++ - 基于 C++ 的 UWP 应用程序中缺少 ShutdownManager 类?
- ruby-on-rails - 对 rails API 的请求仅在该 API 中具有源 IP“11.0.2.1”
- django - Django:根据用户实例检索数据库条目
- python - 您如何查找子字符串是否不在字符串中作为正则表达式?
- reactjs - 可折叠问题,我想一次只打开一个问题
- c++ - Visual Studio 2017 c++ 符号未从 Microsoft 服务器加载
- pytorch - 如何使pytorch中的卷积具有关联性?
- excel - 发行抵消lastrow