Skip to content

Commit

Permalink
Use function form of useState
Browse files Browse the repository at this point in the history
This change is for consistency and also so that you can modify the
selected ids multiple times during the same render and get the expected
results.
  • Loading branch information
canac committed Oct 23, 2024
1 parent beaeb08 commit efdc2ae
Showing 1 changed file with 14 additions and 9 deletions.
23 changes: 14 additions & 9 deletions src/hooks/useMassSelection.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,21 +23,26 @@ export const useMassSelection = (idsList: string[]): UseMassSelectionResult => {
}, [idsList]);

const toggleSelectionById = (id: string) => {
if (ids.includes(id)) {
setIds((previousIds) =>
previousIds.filter((selectedIds) => selectedIds !== id),
);
} else {
setIds((previousIds) => [...previousIds, id]);
}
setIds((previousIds) => {
if (previousIds.includes(id)) {
return previousIds.filter((selectedIds) => selectedIds !== id);
} else {
return [...previousIds, id];
}
});
};

const selectMultipleIds = (newIds: string[]) => {
setIds([...ids, ...newIds.filter((newId) => !ids.includes(newId))]);
setIds((previousIds) => [
...previousIds,
...newIds.filter((newId) => !ids.includes(newId)),
]);
};

const deselectMultipleIds = (idsToRemove: string[]) => {
setIds(ids.filter((id) => !idsToRemove.includes(id)));
setIds((previousIds) =>
previousIds.filter((id) => !idsToRemove.includes(id)),
);
};

const deselectAll = () => {
Expand Down

0 comments on commit efdc2ae

Please sign in to comment.