Overview
reatomSet creates an atom that manages Set state with built-in methods for common set operations like add, delete, toggle, and clear. It also provides a reactive size computed property.
Import
Type Signature
Parameters
StateInit<T>
default:"new Set()"
The initial Set value. Can be a Set instance or an iterable of values
string
default:"'setAtom'"
Optional name for the atom, useful for debugging
Properties
size
A computed atom that reactively tracks the size of the set. Type:Computed<number>
Methods
add
Adds an element to the set. Only triggers an update if the element is not already present.T
The element to add
Set<T> - The updated set
delete
Removes an element from the set.T
The element to remove
Set<T> - The updated set
toggle
Toggles the presence of an element in the set. Adds it if not present, removes it if present.T
The element to toggle
Set<T> - The updated set
clear
Removes all elements from the set. Returns:Set<T> - The cleared set
reset
Resets the set to its initial state. Returns:Set<T> - The reset set
set
Replaces the entire set state. Accepts a Set instance, an array, or a function.StateInit<T> | ((current: Set<T>) => Set<T>)
New state value or update function
Set<T> - The new set
Basic Usage
Advanced Usage
Using Array Syntax for Initialization
Managing Selected Items
Reactive Size and Empty State
Using with Complex Objects
Reset to Initial State
Notes
- All mutations create new Set instances, preserving immutability
- The
add,delete, andtogglemethods only trigger updates when the set actually changes - The
sizeproperty is a computed atom that automatically updates when the set changes - Set equality is based on
===comparison, which means object references must match - Direct mutations like
setAtom().add('value')will not trigger updates; use the provided methods instead - The
set()method can accept both Set instances and arrays for flexibility