I'm facing a problem with react-table v7.8.0 and useRowSelect plugin. All I want is to toggle the selection mode by setting a state variable, so that I'm able to show or hide the additional selection column just by clicking a button like "toggle selection mode for table".
That's what I'm trying in my code:
const {
getTableProps,
getTableBodyProps,
headerGroups,
prepareRow,
rows,
state,
page,
canPreviousPage,
canNextPage,
pageOptions,
pageCount,
gotoPage,
nextPage,
previousPage,
setPageSize,
allColumns,
state: { pageIndex, pageSize, selectedRowIds },
preGlobalFilteredRows,
setGlobalFilter
} = useTable({ columns,
data,
},
useGlobalFilter,
useSortBy,
usePagination,
useRowSelect,
hooks => {
hooks.visibleColumns.push(columns => {
return [
(tableRowsSelectable === true ? {
id: 'selection',
Header: ({getToggleAllRowsSelectedProps}) => (
<div><Checkbox {...getToggleAllRowsSelectedProps()} /></div>
),
Cell: ({row}) => (<div><Checkbox {...row.getToggleRowSelectedProps()} /></div>)
}: []),
...columns,
]})
})
The tableRowsSelectable, that is set as property (either true or false) for the react-table component, is my attempt to control the selection mode. But that does not work. Nothing happens, when I change tableRowsSelectable.
By the way: Do not consider Checkbox component. It is just a custom component and works as expected.
What else can I do?