Overview
TheenableReactTracking function configures how Legend-State tracks observables in React components. It can enable automatic tracking, provide development warnings, and help catch common mistakes.
Signature
Configuration options
Automatically wrap
get() calls in React components with useSelector(). When enabled, you don’t need to use observer() or hooks.Warn in development when
get() is called in an unobserved component. Helps catch missing observer() wrappers.Warn in development when
get() is used instead of the use$() hook. Helps with React Compiler compatibility.Auto Mode
Withauto: true, all get() calls in React components automatically trigger re-renders. You don’t need to use observer() or useSelector().
Auto Mode Behavior
get()calls inside components automatically useuseSelector()internally- Works with any component, no wrapper needed
- Already-tracked contexts (inside
observer()oruseSelector()) are not double-wrapped - Slightly more overhead per
get()call, but very convenient
Development Warnings
Warn Unobserved
Catch missingobserver() wrappers by warning when get() is called in unobserved components:
Warn Missing use$()
Ensure React Compiler compatibility by warning whenget() is used instead of use$():
The
use$() hook is recommended for React Compiler compatibility. It works the same as useSelector(() => obs$.get()) but with better optimization support.Combined Configuration
You can enable multiple options together:When to Use Each Option
auto: true
Use when:- Rapid prototyping or small apps
- You want the simplest possible API
- Component performance is not critical
- Building large-scale production apps
- You need maximum performance
- You prefer explicit tracking for clarity
warnUnobserved: true
Use when:- Learning Legend-State
- Migrating existing code to use observables
- You want to catch tracking mistakes early
- Working in development mode
- You intentionally use
peek()in some places - The warnings become noisy
warnMissingUse: true
Use when:- Preparing for React Compiler
- You want to enforce
use$()usage - Building a team project with coding standards
- Working in development mode
- Not using React Compiler
- Using
observer()components (no need foruse$()inside)
Implementation Details
Under the hood,enableReactTracking modifies the get() function on all observables to:
- Detect if it’s called inside a React component (using React internals)
- Check if it’s already tracked (e.g., inside
observer()oruseSelector()) - Either automatically wrap with
useSelector()(auto mode) or log warnings (warn modes)
TypeScript Support
No additional types needed - the configuration doesn’t change the API surface, only the runtime behavior.Examples
Development Setup
Prototype Mode
Production Setup
Best Practices
Choose your style: Either use auto mode everywhere, or use explicit tracking everywhere. Mixing styles can be confusing.
Related
- observer - Wrap components for reactive tracking
- useSelector - Track observables in hooks
- use$ - React Compiler-friendly tracking hook
- configureLegendState - Core configuration