Skip to main content

What are Derived Stores?

Derived stores are read-only stores that automatically compute their value based on other stores or atoms. They are perfect for creating computed state that stays in sync with your source data without manual updates. When you create a store or atom with a getter function instead of an initial value, you get a derived (computed) store that:
  • Automatically tracks dependencies
  • Only recomputes when dependencies change
  • Evaluates lazily (only when accessed)
  • Cannot be directly mutated

Creating Derived Stores

Basic Derived Store

Derived stores created with createStore use the .state property to access values from other stores.

Derived Atoms

You can also create derived state using atoms directly:
Atoms use .get() instead of .state for accessing values.

Multiple Dependencies

Derived stores automatically track all dependencies accessed in their getter function:

Chaining Derived Stores

You can create chains of derived stores where each depends on the previous:

Conditional Dependencies

Dependencies are tracked dynamically based on what is actually accessed:

Practical Examples

E-commerce Cart Total

Search and Filter

Form Validation

Dashboard Statistics

Performance Characteristics

Derived stores only compute when their value is accessed. If a derived store isn’t being used, it won’t compute even if its dependencies change.
Derived stores cache their computed value and only recompute when dependencies actually change.
Only stores that are actually affected by a change will recompute, creating an efficient reactive graph.

Best Practices

Keep Computations Pure

Derived stores should not have side effects. They should only compute and return a value based on their dependencies.

Avoid Expensive Operations

If a computation is expensive, consider memoizing intermediate results or using separate derived stores.

Use Specific Dependencies

Only access the specific values you need to minimize unnecessary recomputation.

Chain When Logical

Break complex computations into multiple derived stores for better readability and performance.

Stores

Learn about the base store primitive

Atoms

Lower-level reactive primitives

Subscriptions

React to changes in derived stores

Batching

Optimize multiple updates to source stores