I have component with table of items. I would like to select few items and be able to do some operation on selected items when i click button under table.
My main table component:
export default function TableComp() {
function handleSelectedRows() {...}
return (
<Table>
...
{items.map(item => (
<TableRowItem item={item}></TableRowItem>
))}
</Table>
<Button onClick={handleSelectedRows}/>
)}
My TableRowItem component:
function TableRowItem({item}) {
const [active, setActive] useState(false);
function handleCLick() {
setActive(!active);
}
return (
<TableRow onClick={handleCLick} active={active}>...</TableRow>
)
}
How I could know from Table component what rows was selected?
I could put selected items to the store, but is there any other proper way to get them?
2
Answers
You can store the selected row keys in a state.
stackblitz
My React Table Component you can handle selecting table rows by maintaining state to track which rows are selected. Here’s a basic example of how you can achieve this: