Skip to main content

Function Signature

Creates a reactive store that can hold and manage state. The function has two overloads:
  • When passed a function, it creates a ReadonlyStore with computed/derived state
  • When passed a value, it creates a Store with mutable state

Parameters

T
The initial value for the store. Creates a mutable store that can be updated with setState().
(prev?: NoInfer<T>) => T
A getter function that computes the store’s value. Creates a readonly store that automatically recomputes when dependencies change.
  • prev - The previous computed value (optional)

Returns

Store<T>
Returns a mutable Store instance when initialized with a value. Provides:
  • state - Get the current state
  • get() - Get the current state (alias)
  • setState(updater) - Update the state
  • subscribe(observer) - Subscribe to state changes
ReadonlyStore<T>
Returns a readonly ReadonlyStore instance when initialized with a function. Provides:
  • state - Get the current computed state
  • get() - Get the current computed state (alias)
  • subscribe(observer) - Subscribe to state changes

Examples

Basic Store

Computed/Derived Store

Subscribing to Changes

Complex State Management