Documentation Index
Fetch the complete documentation index at: https://mintlify.com/LegendApp/legend-state/llms.txt
Use this file to discover all available pages before exploring further.
Overview
Observables are the foundation of Legend-State. They are reactive containers that track their values and notify listeners when values change. Observables can wrap any JavaScript value: primitives, objects, arrays, Maps, Sets, and even Promises.Creating Observables
Basic Observable
Create an observable using theobservable() function:
Observable with Undefined
Create an observable without an initial value:Observable Primitive
For performance-critical scenarios with primitive values, useobservablePrimitive():
observablePrimitive() is optimized for primitive values and doesn’t support nested properties.Observable Types
Objects
When you create an observable from an object, each property becomes observable:Arrays
Arrays work like objects, with each index becoming an observable:Maps and Sets
Maps and Sets are fully supported:Booleans
Boolean observables have a specialtoggle() method:
Type Signatures
Observable Interface
Every observable implements the following core methods:The
Observable type automatically infers the correct structure based on your data. Objects become ObservableObject, arrays become ObservableArray, etc.Working with Promises
Observables can wrap Promises, which resolve asynchronously:Best Practices
Use $ Suffix
Conventionally, append
$ to observable variable names to distinguish them from regular values.Avoid Over-nesting
While deep nesting is supported, consider flattening your state structure for better performance.
Type Your Observables
Always provide TypeScript types for better autocomplete and type safety.
Use Primitives Wisely
Use
observablePrimitive() for counters, flags, and other simple values that change frequently.Next Steps
Getting and Setting
Learn how to read and update observable values
Observing Changes
Set up listeners to react to changes