How to use setSortBy in react-table

162 Views Asked by At

While using the setSortBy function. It is showing the below error "setSortBy is not a function".

How im accesssing setSortBy:

const { getTableProps, getTableBodyProps, headerGroups, rows, setSortBy, } = useTable( { columns, data, defaultColumn, initialState: { pageIndex: 0 }, }, usePagination, useBlockLayout, useResizeColumns );

How im using: <span onClick={() => setSortBy([{ id: column.id, desc: false }])}> <span onClick={() => setSortBy([{ id: column.id, desc: true }])}>

1

There are 1 best solutions below

0
vigneshwaran On

It is because, you need to include useSortBy in your useTable like this

const { getTableProps, getTableBodyProps, headerGroups, rows, setSortBy, } = useTable({
 columns,
 data,
 defaultColumn,
 initialState: { pageIndex: 0 },
 },
 usePagination,
 useBlockLayout,
 useResizeColumns,
 useSortBy
 );

without including this useSortBy, you cannot able to destructure setSortBy from useTable