useSet

Manage a Set state.

https://www.shaktools.com/shak-hooks
State

API Reference

Installation

terminal
pnpm add @shak-hooks/usehooks
import.ts
import { useSet } from "@shak-hooks/usehooks";
Source: packages/react/src/useSet.ts

Signature

signature.ts
export function useSet<K>(initialValue?: Iterable<K>)

Parameters

NameTypeOptionalDefault
initialValueIterable<K>Yes-

Returns

unknown

Implementation

use-set.ts
1import { useState, useCallback } from "react";
2
3export function useSet<T>(initialValue: Set<T> = new Set()) {
4 const [set, setSet] = useState(initialValue);
5
6 const add = useCallback((item: T) => {
7 setSet((prev) => {
8 const copy = new Set(prev);
9 copy.add(item);
10 return copy;
11 });
12 }, []);
13
14 const remove = useCallback((item: T) => {
15 setSet((prev) => {
16 const copy = new Set(prev);
17 copy.delete(item);
18 return copy;
19 });
20 }, []);
21
22 const reset = useCallback(() => {
23 setSet(initialValue);
24 }, [initialValue]);
25
26 return [set, { add, remove, reset }] as const;
27}

Advertisement

Google Ads

Usage

example.tsx
1import { useSet } from "@shak-hooks/usehooks";
2
3const result = useSet(undefined);
4// result: unknown
5// Use values directly (React state).

Let‘s do great work together

Empowering creators with free, high-performance AI, SEO, and developer tools. Join thousands of users optimizing their workflow with Shak-Tools.

Tools10+ Free
UsersGlobal

Stay in the loop

Join our newsletter for the latest AI tools and updates.

ShakTools
© 2025 Shaktools. All Rights Reserved.Privacy Policy