【发布时间】:2020-07-27 03:39:29
【问题描述】:
我正在实现一个名为 Table 的组件,它是从 react-bootstrap 导入的。我目前正在通过数组将数据从父级传递给子级,但是我在通过父类编写数组时遇到了困难,在这种情况下我不知道如何编写代码
这是我在文件夹index.js 中的代码src/components/table/index
import React from 'react';
import {Table, Image} from 'react-bootstrap';
import '../Table/index.css';
import Button from '../Button/index';
const TableItem = ({productList}) => {
const redirectToEdit = () => {
console.log("Open edit form for me, please?")
}
const deleteProductItem = () => {
console.log("The product has been deleted")
}
return(
<Table striped bordered hover>
<thead>
<tr>
<th>No. </th>
<th>Image</th>
<th>Name</th>
<th>Category</th>
<th>Price</th>
<th>Action</th>
</tr>
</thead>
<tbody>
{productList.map((product, index) => (
<tr key={index}>
<td>{product.id}</td>
<td><Image src={product.image} /></td>
<td>{product.name}</td>
<td>{product.category}</td>
<td>{product.price}</td>
<td>
<Button variant="success" onClick={redirectToEdit}>Edit</Button>
<Button variant="danger" onClick={deleteProductItem}>Delete</Button>
</td>
</tr>
))}
</tbody>
</Table>
);
}
export default TableItem;
这是我在app.js文件夹src/app.js中的代码
<TableItem
productList={["1", "https://place-hold.it/100x150", "Black milk tea", "Milk Tea", "10000"]}
/>
在上面,我只是添加了 1 个产品,但运行时不显示,我的问题是 如何在app.js 中为此编写许多产品通过 数组
谁能帮帮我,非常感谢?
【问题讨论】:
标签: reactjs components react-bootstrap-table