Skip to content

useBulkSelection

Manages bulk selection state for collections of items.

Signature

ts
function useBulkSelection<T extends SelectableId = SelectableId>(
  options?: UseBulkSelectionOptions<T>
): UseBulkSelectionReturn<T>

Parameters

UseBulkSelectionOptions<T>

PropertyTypeDefaultDescription
initialSelectedT[][]Initially selected IDs

Return Type

UseBulkSelectionReturn<T>

PropertyTypeDescription
selectedIdsSet<T>Set of currently selected IDs
selectedCountnumberNumber of selected items
isSelected(id: T) => booleanCheck if an ID is selected
select(id: T) => voidAdd an ID to selection
deselect(id: T) => voidRemove an ID from selection
toggle(id: T) => voidToggle an ID's selection state
selectAll(ids: T[]) => voidReplace selection with given IDs
clear() => voidClear all selections
isAllSelected(ids: T[]) => booleanCheck if all given IDs are selected
isSomeSelected(ids: T[]) => booleanCheck if some (but not all) IDs are selected

Types

ts
type SelectableId = string | number

Example

tsx
const { selectedIds, toggle, isSelected, clear } = useBulkSelection<number>()

<Checkbox checked={isSelected(item.id)} onChange={() => toggle(item.id)} />

See also