Overview
Signals are a reactive primitive that provide fine-grained reactivity for managing state in Angular applications. They enable automatic dependency tracking and efficient change detection.Core Concepts
Writable Signals
Create signals using thesignal() function to hold reactive values.
Reading Signal Values
Access signal values by calling them as functions.Computed Signals
Create derived values that automatically update when dependencies change.Computed signals are read-only and automatically recompute only when their dependencies change, making them highly efficient.
Effects
Execute side effects when signals change using theeffect() function.
Effect Cleanup
Register cleanup functions to handle resource disposal.Advanced Patterns
Signal-based State Management
Using the Store
RxJS Interop
Convert between signals and observables for integration with existing RxJS code.Best Practices
Use Computed for Derived State
Prefer
computed() over manually updating multiple signals. It’s more efficient and easier to maintain.Keep Signals Fine-Grained
Split state into focused signals rather than one large object signal for better granularity.
Effects for Side Effects Only
Use
effect() for side effects, not for deriving state. Use computed() for derived values.Immutable Updates
Use immutable patterns with
update() to ensure predictable state changes.Performance Benefits
Signals provide significant performance improvements:- Fine-grained updates: Only components reading changed signals are updated
- Automatic dependency tracking: No manual subscription management
- Efficient computation: Computed values only recalculate when dependencies change
- Works with OnPush: Seamlessly integrates with
OnPushchange detection
