Map and Set in Zustand
Map and Set are mutable data structures. To use them in Zustand, you must create new instances when updating.
Map
Reading a Map
Updating a Map
Always create a new Map instance:
Set
Reading a Set
Updating a Set
Always create a new Set instance:
Why New Instances?
Zustand detects changes by comparing references. Mutating a Map or Set doesn't change its reference:
Pitfall: Type Hints for Empty Collections
Provide type hints when initializing empty Maps and Sets:
Without type hints, TypeScript infers never[] which prevents adding items later.