Vize

use-set

Create a reactive Set with typed helpers.

Package @vizejs/composable/use-set
Own the source vize lib pull composable:use-set
Runtime exports useSet
Gzip budget 1280 B

Usage

import { useSet } from "@vizejs/composable/use-set";

Runtime contract

Utility Category Stability SSR Hydration Cleanup Targets Host globals Uses
useSet state experimental safe stable none web, server, worker, native, desktop, terminal — —

API

useSet

Create a reactive Set with typed helpers. The member type is inferred from the initial items (useSet(["a"]) is a Set<string>); pass a type argument for an initially empty set. Membership checks are tracked per item. The set-algebra helpers return new plain sets and do not require ES2025 Set methods. Purely synchronous state: safe during server rendering (no host globals, no timers) and nothing to dispose.

function useSet<Item = unknown>(initial: Iterable<Item> = []): SetControls<Item>
const selected = useSet<number>();
selected.toggle(3); // true
selected.has(3); // true

Types

SetControls

Reactive set wrapper returned by useSet.

Member Type Description
set ReadonlySet<Item> Read-only reactive view of the set. has, iteration, and size on this view are tracked. Members are stored shallowly.
size ComputedRef<number> Number of members.
values ComputedRef<readonly Item[]> Snapshot of the members in insertion order.
has (item: Item) => boolean Whether item is a member. Reactive when read inside an effect.
add (item: Item) => boolean Add item.
delete (item: Item) => boolean Remove item.
toggle (item: Item, force?: boolean) => boolean Add item when absent and remove it when present, or force membership. Passing an explicit undefined behaves like passing no argument.
clear () => void Remove every member.
union (other: Iterable<Item>) => Set<Item> Members of this set or other.
intersection (other: Iterable<Item>) => Set<Item> Members of this set that are also in other.
difference (other: Iterable<Item>) => Set<Item> Members of this set that are not in other.
reset () => void Restore the creation-time members.