So I'd like use react-table to create a sortable column that numbers each row (according to its position in the data set).
According to docs, sorting is done via a sorting function that compares the accessor
value. However, the accessor
option does not expose the index, unlike the Cell
option.
So that means this won't work:
const columns = [
{
Header: '#',
id: 'index',
accessor: (row) => row.index // 'index' is undefined
},
/* ... */
]
My current workaround is to inject an index directly into the dataset like this:
myIndexedData = myData.map((el,index) => ({index, ...el})) //immutable
return (
<ReactTable
data={myIndexedData}
columns={columns}
/>
);
This is not really an optimal solution, especially with a large dataset. Is there a better way that I'm not seeing?