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

问题描述 投票:0回答:1

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

        <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 />;
        }
      }
]

我想将功能作为道具从我的主App.jsx文件传递到DescriptionCell组件。此功能将用于在DescriptionCell内部执行一些onClick功能。

我该怎么做?

谢谢。

react-table
1个回答
0
投票
您可以使用传递到column组件的Cell道具来完成:

columns: [ ... { Header: "Description", accessor: "Description", onClick: () => { alert('click!'); }, Cell: (props) => { return ( <DescriptionCell onClick={props.column.onClick} country={props.row.values.Currency}> {String(props.cell.value)} </DescriptionCell> ); }, }

或者如果您的函数可能被其他许多单元使用(例如更新您的后端),则可以将该函数传递给主要的Table组件,如本例所示:https://github.com/tannerlinsley/react-table/blob/master/examples/kitchen-sink-controlled/src/App.js#L622
© www.soinside.com 2019 - 2024. All rights reserved.