Skip to Content
ohooks

useSet

Manages a reactive Set in React state with immutable update helpers.

Loading preview...

The useSet hook manages a JavaScript Set in React state and provides memoized helper methods (add, remove, toggle, has, clear, and reset) that clone the set on mutations so components re-render predictably.

Installation

pnpm dlx shadcn@latest add https://ohooks.vercel.app/r/useSet.json

Usage

Examples

Multi-Select Tag Filter

Use toggle and has to build a multi-select filter bar where clicking a tag adds or removes it from the active set.

Tracking Expanded Accordion Items

Store expanded item IDs in useSet to allow multiple accordion panels to stay open simultaneously.

API Reference

UseSetActions

Memoized set mutation and lookup methods returned as the second tuple element.

Property
Specification
add
function
Adds value to the set if not already present.
remove
function
Removes value from the set if present.
toggle
function
Adds value if absent, or removes it if present.
clear
function
Removes all values from the set.
reset
function
Resets the set back to initialValue.
has
function
Returns true if value is in the set.