Function Signature
- When passed a function, it creates a ReadonlyStore with computed/derived state
- When passed a value, it creates a Store with mutable state
Parameters
The initial value for the store. Creates a mutable store that can be updated with
setState().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
Returns a mutable
Store instance when initialized with a value. Provides:state- Get the current stateget()- Get the current state (alias)setState(updater)- Update the statesubscribe(observer)- Subscribe to state changes
Returns a readonly
ReadonlyStore instance when initialized with a function. Provides:state- Get the current computed stateget()- Get the current computed state (alias)subscribe(observer)- Subscribe to state changes
Examples
Basic Store
Computed/Derived Store
Subscribing to Changes
Complex State Management
Related APIs
- Store Class - Methods available on Store instances
- ReadonlyStore Class - Methods available on ReadonlyStore instances
- createAtom - Lower-level primitive for reactive values