Skip to main content

Class Overview

The 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

Gets the current state value.
T
The current state stored in the store.

Methods

get()

Gets the current state value. This is an alias for the state property.
T
The current state stored in the store.

setState()

Updates the state. The updater function receives the previous state and should return the new state.
(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()

Subscribes to state changes. The callback is invoked whenever the state changes.
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

  • createStore - Factory function to create stores
  • ReadonlyStore - Read-only variant without setState
  • Atom - Lower-level reactive primitive
  • batch - Batch multiple updates together