reactjs - React-Hooks:如何创建可重用的数据表组件?
问题描述
在我的应用程序中,我需要可重用的数据表组件。我可以在哪里使用动态内容更改表头和表体。
<table className="table table-hover">
<thead>
<tr>
<th>#</th>
<th>Image</th>
<th>Title</th>
<th>Publish Date</th>
</tr>
</thead>
<tbody>
{data &&
data.map(item => (
<tr key={item.id}>
<td>{item.id}</td>
<td>{item.image}</td>
<td>{item.title}</td>
<td>{item.publishDate}</td>
</tr>
))}
</tbody>
</table>
解决方案
这可能会有所帮助
const CustomTable = ({header, posts}) => {
return (
<table>
<thead>
<tr>
<th>#</th>
<th>{header.image}</th>
<th>{header.title}</th>
<th>{header.publishedDate}</th>
</tr>
</thead>
<tbody>
{posts &&
posts.map(item => (
<tr key={item.id}>
<td>{item.id}</td>
<td>{item.image}</td>
<td>{item.title}</td>
<td>{item.publishDate}</td>
</tr>
))}
</tbody>
</table>
);
}
您可以在任何需要的地方传递 header 和 posts 数组。
<div className='table'>
<CustomTable header={header} posts={posts} />
</div>
推荐阅读
- r - geom_dotplot 中未对齐的点
- java - 我可以编写一个休眠实体吗?
- c# - Azure Functions 的 HostingEnviornment.QueueBackgroundWorkItem 等效项
- mysql - 在 MySql 中使用查询查找排名
- charts - 具有正常和对数刻度的剑道图表
- bash - 如何制作 git checkout
仅显示我在本地拥有的分支机构 - c++ - 如何仅使用 SSE2 以双倍地板/整数?
- r - 如何使用fluidRow和mainPanel使文本居中?
- python - 多字选项
- c# - ASP.NET MVC 5 - 模型中有 HTML 内容的奇怪行为