reactjs - 如何从应用程序组件访问我的反应可重用表格单元格数据
问题描述
我创建了一个表格组件,因此我可以在项目的不同部分重用它,因为我需要它更灵活,并且更好地实践如何做到这一点 我的目标:在我的朋友组件中我正在尝试捕捉用户点击的单元格。
好友组件
const Friends = () => {
// My goal is to get the the information about the clickedcell here
const theadData = ["Name", "Email", "Date"];
const tbodyData = [
{
id: "1",
items: ["Peter", "peter@email.com", "01/01/2021"],
},
{
id: "2",
items: ["Foo", "foo@email.com", "12/24/2020"],
},
{
id: "3",
items: ["bar", "bar@email.com", "12/01/2020"],
},
];
return(
<div>
<Table theadData={theadData}
tbodyData={tbodyData} />
</div>
)
}
export default Friends;
表组件
import React from 'react'
import TableHeaderItem from './tableHead'
import TableRow from './tableRow'
const Table = ({theadData, tbodyData}) => {
return (
<table>
<thead>
<tr>
{
theadData.map(headerTitle => {
return <TableHeaderItem
key={headerTitle}
item={headerTitle} />
})
}
</tr>
</thead>
<tbody>
{
tbodyData.map(row => {
console.log("rowID", row.id)
return <TableRow
onRowClicked={() => console.log('user clicked on row #', row.id)}
key={row.id}
rowData={row.items} />
})
}
</tbody>
</table>
)
}
export default Table
表格行组件
import TableCell from './tableCell'
const TableRow = ({ rowData, onRowClicked, onCellClick}) => {
return(
<tr
onClick={onRowClicked} >
{
rowData.map(cell => {
return (
<TableCell
key={cell}
cellData={cell}
onCellClicked={()=>console.log('user cliecked on cell name',cell)}
/>)})
}
</tr>
)
}
export default TableRow;
表格单元组件
const TableCell = ({ cellData, onCellClicked}) => {
return (
<td
onClick={onCellClicked}>{cellData}</td>
)
}
export default TableCell;
解决方案
好友组件
const Friends = () => {
// My goal is to get the the information about the clickedcell here
const theadData = ["Name", "Email", "Date"];
const tbodyData = [
{
id: "1",
items: ["Peter", "peter@email.com", "01/01/2021"],
},
{
id: "2",
items: ["Foo", "foo@email.com", "12/24/2020"],
},
{
id: "3",
items: ["bar", "bar@email.com", "12/01/2020"],
},
];
const onFriendClicked = (e) => {
console.log(e.target);
}
return(
<div>
<Table theadData={theadData}
tbodyData={tbodyData}
onTableClicked={onFriendClicked}
/>
</div>
)
}
export default Friends;
表组件
import React from 'react'
import TableHeaderItem from './tableHead'
import TableRow from './tableRow'
const Table = ({theadData, tbodyData, onTableClicked}) => {
return (
<table>
<thead>
<tr>
{
theadData.map(headerTitle => {
return <TableHeaderItem
key={headerTitle}
item={headerTitle} />
})
}
</tr>
</thead>
<tbody>
{
tbodyData.map(row => {
console.log("rowID", row.id)
return <TableRow
onRowClicked={onTableClicked}
key={row.id}
rowData={row.items} />
})
}
</tbody>
</table>
)
}
export default Table
表格行组件
import TableCell from './tableCell'
const TableRow = ({ rowData, onRowClicked}) => {
return(
<tr
onClick={onRowClicked} >
{
rowData.map(cell => {
return (
<TableCell
key={cell}
cellData={cell}
onCellClicked={onRowClicked}
/>)})
}
</tr>
)
}
export default TableRow;
表格单元组件
const TableCell = ({ cellData, onCellClicked}) => {
return (
<td
onClick={onCellClicked}>{cellData}</td>
)
}
export default TableCell;
推荐阅读
- javascript - 为什么 object[bar] 与 object[foo] 有相同的输出?
- c - 在 VxWorks 下创建串行环回
- javascript - 对象的递归路径
- ios - 如何在swift 3中搜索属性文本中的单词?
- java - 如何让android直接连接mysql
- dart - Angular Dart“reorder-list”组件使用示例
- r - highcharter:BoxPlot 异常值不在正确的 x 轴点上
- scala - 编写火花作业时是否有理由不使用 SparkContext.getOrCreate?
- c# - 使用 s22 库仅读取 IMAP 中的主要电子邮件
- python - Scipy 最小化不执行迭代