I have a table (parent element) which fetches users and render each row as it's own component. Row in a table contains checkbox.
Goal is to be able to use checkboxes and retrieve checked checkboxes.
Problem is that when i'm passing function to each row (child component), to trigger adding checked checkbox value to the array (which consists of id's of selected users), whole component gets re-rendered and it is really laggy on the front-end (obviously). I tried to create refs for every checkbox to retrieve checked checkboxes only when needed - but you can't use Refs in a loop and seem to be not a good thing to do for such a task.
My question is how to handle lots of checkboxes in one table, storing checked checkboxes in a state or being able to retrieve checked checkboxes when triger a function in parent component?
const UserRow = ({checked, setChecked) => (
// render fields
// ...
<Checkbox checked={checked} onChange={() => setChecked()} />
)
const ParentComponent = () => {
const [users, setUsers] = useState();
const [checkedCheckboxes, setCheckedCheckboxes] = useState([]);
useEffect(() => {
// fetch users
setUsers(fetchedUsers);
});
const rows = users.map(user => <UserRow
key={id}
name={user.name}
email={user.email}
checked={checkedCheckboxes.includes(id)}
setChecked={setCheckedCheckboxes(checkedCheckboxes.concat(id)}
/>;
return (
// render table
{rows} // inset rows
)}