How to use row State in v8 like useRowState in v7? #4140
Replies: 4 comments 3 replies
-
I'm interested in finding a solution for this as well. My use case is that I want to add some row-level toggles that modify each column in a different way. I'd prefer to not have to store these properties in the table Some ideas I've tried to solve this in React (without success):
function TableRow<TRow>({ row }: { row: Row<TRow> }) {
const [rowState, setRowState] = useState(1);
return (
<tr>
{row.getVisibleCells().map((cell) => (
<td id={cell.id}>
{flexRender(cell.column.columnDef.cell, {
...cell.getContext(),
rowState,
})}
</td>
))}
</tr>
);
} The problem with this is that, at least in TypeScript, the type definition of the columnHelper.accessor("attr", {
cell: (info) => info.rowState === 1 ? "this" : "that", // Error TS2339: Property 'rowState' does not exist on type 'CellContext<...>'
}),
const [rowState, setRowState] = useState({});
const columns = useMemo(() => [
columnHelper.accessor("attr", {
cell: (info) => rowState[info.row.id] === 1 ? "this" : "that",
}),
], [rowState]); Modifying the column definitions on state changes is slow in its own way, and I'm not convinced it's even a smart thing to do as it risks losing other table state such as row expansion. This also has its own performance issues, because the The library has a few ways it tracks row state internally, such as row selection and expansion, but I don't see a way to extend it to allow for custom row state. |
Beta Was this translation helpful? Give feedback.
-
I'm into the same problem. I need to know when the row is hover: true to change the sell appearance. |
Beta Was this translation helpful? Give feedback.
-
Let me know if there is another solution but this worked for me:
import {
createColumnHelper,
FilterFn,
flexRender,
getCoreRowModel,
getFilteredRowModel,
Row,
Table as TableType,
useReactTable,
CellContext as TanCellContext,
RowData,
} from "@tanstack/react-table";
const [isMouseOverRowId, setIsMouseOverRowId] = useState("");
columnHelper.accessor("columnId", {
header: () => "Status",
cell: (item: unknown) => {
const itemCasted = item as CellContext<Row<Item>, boolean>;
return <YourComponent hover={itemCasted.hover} />
},
}), <tbody>
{rows.map((row) => {
return (
<Tr
key={row.id}
onMouseEnter={() => {
setIsMouseOverRowId(row.id);
}}
onMouseLeave={() => {
setIsMouseOverRowId("");
}}
>
{row.getVisibleCells().map((cell) => (
<Td key={cell.id}>
{flexRender(cell.column.columnDef.cell, {
...cell.getContext(),
hover: row.id === isMouseOverRowId,
})}
</Td>
))}
</Tr>
); |
Beta Was this translation helpful? Give feedback.
-
I'm doing some experiments using the approach shown in the Editable data example. Personally I'm not a fan of this solution but It's the only working that I found so far. In particular I'm afraid of performances since as far as I know react-table had / has some difficulties handling whole table re-renders, see #4794 and #2824. Thoughts or suggestions on this are welcome! |
Beta Was this translation helpful? Give feedback.
-
I`m using
row.setState
in v7 like this.API Reference
How to using it in v8?
Beta Was this translation helpful? Give feedback.
All reactions