Class Overview
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
T
The current computed state value.
Methods
get()
state property.
T
The current computed state value.
subscribe()
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
Related APIs
- createStore - Factory function to create stores
- Store - Mutable variant with setState
- ReadonlyAtom - Lower-level computed primitive