Overview
reatomMap creates an atom that manages Map state with built-in methods for common map operations like set, delete, and clear. It also provides a reactive size computed property and utilities like getOrCreate.
Import
Type Signature
Parameters
StateInit<Key, Value>
default:"new Map()"
The initial Map value. Can be a Map instance or an array of key-value pairs
string
default:"'mapAtom'"
Optional name for the atom, useful for debugging
Properties
size
A computed atom that reactively tracks the size of the map. Type:Computed<number>
Methods
setState
Updates the entire map state. Can accept a new state directly or a function that receives the current state.StateInit<Key, Value> | ((state: Map<Key, Value>) => StateInit<Key, Value>)
New state value or update function
Map<Key, Value> - The new state
set
Sets a key-value pair in the map. Only triggers an update if the value has changed.Key
The key to set
Value
The value to associate with the key
Map<Key, Value> - The updated map
delete
Removes a key from the map.Key
The key to delete
Map<Key, Value> - The updated map
clear
Removes all entries from the map. Returns:Map<Key, Value> - The cleared map
reset
Resets the map to its initial state. Returns:Map<Key, Value> - The reset map
getOrCreate
Gets a value by key, or creates it if it doesn’t exist using the provided creator function.Key
The key to look up
() => Value
Function to create the value if the key doesn’t exist
Value - The existing or newly created value
Basic Usage
Advanced Usage
Using Array Syntax for Initialization
Cache with getOrCreate
Type-Safe Maps
Reactive Size Tracking
Notes
- All mutations create new Map instances, preserving immutability
- The
setmethod usesObject.is()to compare values and avoid unnecessary updates setStateaccepts both Map instances and arrays of entries for flexibility- The
sizeproperty is a computed atom that automatically updates when the map changes - Direct mutations like
mapAtom().set('key', 'value')will not trigger updates; use the provided methods instead