gpt4 book ai didi

react-table - 如何将自定义 Prop 从 App 传递到 react-table v7 的单元格?

转载 作者:行者123 更新时间:2023-12-03 15:29:41 28 4
gpt4 key购买 nike

这就是我渲染表格主体的方式:

        <tbody {...getTableBodyProps()}>
{rows.map((row, i) => {
prepareRow(row);
return (
<Row {...row.getRowProps()}>
{row.cells.map((cell) => {
// return <td {...cell.getCellProps()}>{cell.render("Cell")}</td>;
return cell.render("Cell");
})}
</Row>
);
})}
</tbody>

这就是我设置列的方式。我为每个单元格创建了独特的组件。

[
{
Header: "Main Header",
Footer: "Foot",
columns: [
{
Header: "Code",
accessor: "NominalCode",
Cell: (props) => {
return <CodeCell>{props.cell.value}</CodeCell>;
},
Footer: () => {
return <FooterTotalCell>Total</FooterTotalCell>;
}
},
{
Header: "Description",
accessor: "Description",
Cell: (props) => {
return (
<DescriptionCell country={props.row.values.Currency}>
{String(props.cell.value)}
</DescriptionCell>
);
},
Footer: () => {
return <td />;
}
}
]

我想将一个函数作为 Prop 从我的主 App.jsx 文件传递​​给 DescriptionCell成分。此函数将用于在 DescriptionCell 中执行一些 onClick 功能.

我怎样才能做到这一点?

谢谢。

最佳答案

您也可以在 Cell 上执行此操作来自 Cell通过将 props 对象直接传递给渲染函数:

...
{rows.cells.map(cell => cell.render('Cell', { test: 'this is a test'}))}
...
然后在您的列定义中
columns: [
...
{
Cell: (props) => {
console.log(props.test) // the value is 'this is a test'
return (
<CustomComponent test={props.test} />
);
},
}

关于react-table - 如何将自定义 Prop 从 App 传递到 react-table v7 的单元格?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/60320390/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com