提问人:Vadim 提问时间:11/6/2023 最后编辑:Vadim 更新时间:11/6/2023 访问量:47
Tanstack React Table 分页无限循环
Tanstack React Table Infinite Loop In Pagination
问:
当我单击分页按钮(下一个、上一个、第一个、最后一个)时,我在 tanstack/react-table 中使用分页时遇到了问题,我的网站由于无限循环而停止响应。 这是我在调试时在控制台中得到的:在此处输入图像描述
这是我的代码:
const columns = useMemo<ColumnDef<BusinessOptions>[]>(
() => [
{
header: 'Info',
accessorKey: 'info',
cell: (info) => info.getValue(),
},
{
header: 'Compiled',
accessorKey: 'compiled',
cell: (info) => info.getValue(),
},
{
header: 'Created At',
accessorKey: 'createdAt',
cell: (info) => info.getValue(),
},
{
header: 'Compiled At',
accessorKey: 'compiledAt',
cell: (info) => info.getValue(),
},
],
[]
);
const table = useReactTable({
data,
columns,
getCoreRowModel: getCoreRowModel(),
getPaginationRowModel: getPaginationRowModel(),
debugTable: true,
});
我显示带有分页菜单的表格的代码:
<div>
<table>
<thead>
{table.getHeaderGroups().map((headerGroup) => (
<tr key={headerGroup.id}>
{headerGroup.headers.map((header) => (
<th key={header.id}>
{header.isPlaceholder
? null
: flexRender(
header.column.columnDef.header,
header.getContext()
)}
</th>
))}
</tr>
))}
</thead>
<tbody>
{table.getRowModel().rows.map((row) => (
<tr key={row.id}>
{row.getVisibleCells().map((cell) => (
<td key={cell.id}>
{flexRender(cell.column.columnDef.cell, cell.getContext())}
</td>
))}
</tr>
))}
</tbody>
</table>
<div className='flex items-center gap-2'>
<button
className='rounded border p-1'
onClick={() => table.setPageIndex(0)}
disabled={!table.getCanPreviousPage()}
>
{'<<'}
</button>
<button
className='rounded border p-1'
onClick={() => table.previousPage()}
disabled={!table.getCanPreviousPage()}
>
{'<'}
</button>
<button
className='rounded border p-1'
onClick={() => table.nextPage()}
disabled={!table.getCanNextPage()}
>
{'>'}
</button>
<button
className='rounded border p-1'
onClick={() => table.setPageIndex(table.getPageCount() - 1)}
disabled={!table.getCanNextPage()}
>
{'>>'}
</button>
<span className='flex items-center gap-1'>
<div>Page</div>
<strong>
{table.getState().pagination.pageIndex + 1} of{' '}
{table.getPageCount()}
</strong>
</span>
<select
value={table.getState().pagination.pageSize}
onChange={(e) => {
table.setPageSize(Number(e.target.value));
}}
>
{[10, 20, 30, 40, 50].map((pageSize) => (
<option key={pageSize} value={pageSize}>
Show {pageSize}
</option>
))}
</select>
</div>
</div>
答: 暂无答案
评论