Skip to main content

useCheckboxGroup

useCheckboxGroup is a hook that reads the nearest Checkbox.Group, such as showing its current selection in a summary.

Import

import { useCheckboxGroup } from '@vortexlabs/vortex-native';

Allied components

import { Checkbox, Text, useCheckboxGroup } from "@vortexlabs/vortex-native";

const SelectedCount = () => {
  const group = useCheckboxGroup();
  if (!group) return null;

  return <Text>Selected: {group.value.length}</Text>;
};

export const Example = () => (
  <Checkbox.Group ariaLabel="Notifications" defaultValue={["email"]}>
    <Checkbox.GroupLabel>Notifications</Checkbox.GroupLabel>
    <Checkbox.Item value="email" label="Email" />
    <Checkbox.Item value="push" label="Push" />
    <SelectedCount />
  </Checkbox.Group>
);

Features

  • Reads the closest group's selection and shared state.
  • Updates when the group value changes.
  • Returns null outside the group.

Group Context

Call useCheckboxGroup() without arguments from a descendant of Checkbox.Group. Inside the group, value is an empty array when nothing is selected.

A component that renders the group must put the hook in a child to read its selection. Use Checkbox.Item for selectable controls. It handles presses and announces the checked state.