Class Overview
Store class is a mutable reactive container that holds state and notifies subscribers when the state changes. It’s created by calling createStore() with an initial value.
Constructor
T
required
The initial value to store. Can be any type.
Properties
state
T
The current state stored in the store.
Methods
get()
state property.
T
The current state stored in the store.
setState()
(prev: T) => T
required
A function that receives the previous state and returns the new state.
prev- The current state value
Always use functional updates with
setState() to ensure you’re working with the latest state value.subscribe()
Observer<T> | ((value: T) => void)
required
Either a callback function that receives the new state, or an observer object with
next, error, and complete methods.{ unsubscribe: () => void }
A subscription object with an
unsubscribe() method to stop receiving updates.Examples
Basic Usage
Managing Object State
Subscribing to Changes
Using Observer Pattern
Array State Management
Conditional Updates
Related APIs
- createStore - Factory function to create stores
- ReadonlyStore - Read-only variant without setState
- Atom - Lower-level reactive primitive
- batch - Batch multiple updates together