Overview
reatomArray creates an atom that manages array state with built-in methods for common array mutations like push, pop, shift, and unshift. All mutations create new array instances to maintain immutability.
Import
Type Signature
Parameters
T[]
default:"[]"
The initial array value
string
default:"'arrayAtom'"
Optional name for the atom, useful for debugging
Methods
push
Adds one or more elements to the end of the array and returns the new length.T[]
Elements to add to the end of the array
number - The new length of the array
pop
Removes and returns the last element from the array. Returns:T | undefined - The removed element, or undefined if the array is empty
shift
Removes and returns the first element from the array. Returns:T | undefined - The removed element, or undefined if the array is empty
unshift
Adds one or more elements to the beginning of the array and returns the new length.T[]
Elements to add to the beginning of the array
number - The new length of the array
Basic Usage
Advanced Usage
Type-Safe Arrays
Using with Atom Methods
SinceArrayAtom extends Atom, you can still use standard atom methods:
Notes
- All mutation methods create new array instances, preserving immutability
- Methods return the same values as their native JavaScript array counterparts
- The atom can be used anywhere a regular
Atom<Array<T>>is expected - Direct mutations like
arrayAtom()[0] = 5will not trigger updates; useset()or the provided methods instead