reactjs - 更改状态值在 react-table 上无法正常工作
问题描述
我正在尝试使用 react-table 为每一行数据添加一个“删除”按钮。按钮拼接数据并在按下时从数据中删除该行。第一次单击“删除”按钮时,它可以正常工作。然而,在第二次按下时没有任何反应。出于某种原因changeData
,不会更改第二次运行的数据。为什么会发生这种情况?
function DataTable({columns, data_rows}) {
const [data, changeData] = useState(data_rows);
if (columns[0].Header !== "Action") {
columns.unshift({
Header: 'Action'
});
}
columns[0]['columns'] = [{
Header: ' ', Cell: ({row}) => (
<ButtonGroup>
<Button className="button-dark button-sm" onClick={() => {
const dataCopy = [...data];
dataCopy.splice(row.index, 1);
changeData(dataCopy);}}>
Remove
</Button>
</ButtonGroup>
)
}]
const {
getTableProps,
getTableBodyProps,
headerGroups,
rows,
prepareRow
} = useTable({
columns,
data
});
return (
<table {...getTableProps()}>
<thead>
{headerGroups.map(headerGroup => (
<tr {...headerGroup.getHeaderGroupProps()}>
{headerGroup.headers.map(column => (
<th {...column.getHeaderProps()}>{column.render("Header")}</th>
))}
</tr>
))}
</thead>
<tbody {...getTableBodyProps()}>
{rows.map((row, i) => {
prepareRow(row);
return (
<tr {...row.getRowProps()}>
{row.cells.map(cell => {
return <td {...cell.getCellProps()}>{cell.render("Cell")}</td>;
})}
</tr>
);
})}
</tbody>
</table>
);
}
解决方案
推荐阅读
- node.js - 使用 node-addon-api 的 node-gyp Node.js 插件不会覆盖 Linux 上的 c++ 版本
- python - 没有名为 arcade 的模块
- mongodb - nodemon] 应用程序崩溃 - 启动前等待文件更改
- go - 使用日志包附加到当前行
- algorithm - 这种贪心算法能更高效吗?
- powershell - 要显示的 powershell 变量值的一部分
- sql - 在数据库的所有现有表中搜索数据
- python - 我有一个状态表,但我希望它每 10 分钟更改一次状态。我正在使用 Postgresql
- r - ifelse R中一系列列的多个OR条件
- javascript - 如果我使用一个端点监视另一个端点,这是不好的做法吗?