Skip to main content

Class Overview

The ReadonlyStore class is a reactive container for computed/derived state. It automatically recomputes its value when dependencies change. Unlike Store, it cannot be directly updated with setState(). It’s created by calling createStore() with a getter function.

Constructor

(prev?: NoInfer<T>) => T
required
A function that computes the store’s value. This function is called to compute the initial value and whenever dependencies change.
  • prev - The previous computed value (optional)

Properties

state

Gets the current computed state value. If the value is stale (dependencies have changed), it will be recomputed automatically.
T
The current computed state value.

Methods

get()

Gets the current computed state value. This is an alias for the state property.
T
The current computed state value.

subscribe()

Subscribes to state changes. The callback is invoked whenever the computed value changes.
Observer<T> | ((value: T) => void)
required
Either a callback function that receives the new computed value, 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 Computed Store

Deriving from Multiple Stores

Filtering and Transforming Data

Subscribing to Computed Values

Complex Computed State

Using with Previous Value

Key Differences from Store

ReadonlyStore vs Store

  • ReadonlyStore: Created with a getter function, automatically recomputes when dependencies change, no setState() method
  • Store: Created with an initial value, manually updated with setState(), mutable