Basic Usage
Function Signature
- Immediately on component mount
- Whenever any signal read inside the effect changes
- During or after the component rerenders (as needed)
Automatic Dependencies
Effects automatically track signal dependencies:Effect Execution Model
When Effects Run
Effects Without Component Rerender
Effects can run even when the component doesn’t rerender:Common Use Cases
Logging and Debugging
Side Effects
Synchronizing with External Systems
Validation
Effect Handle
TheEffect handle allows manual control:
Avoiding Infinite Loops
Be careful when reading and writing the same signal:Effects vs use_future
use_effect
- Synchronous
- Reruns automatically when dependencies change
- For side effects and synchronization
use_future
- Asynchronous
- Returns a value via
use_resourceor runs indefinitely - For async operations like fetching data
Multiple Effects
You can use multiple effects in one component:Cleanup
Effects don’t have explicit cleanup. For cleanup behavior, use spawned tasks:use_on_destroy:
Performance Considerations
- Effects are deduplicated - if triggered multiple times in the same tick, they run once
- Effects run after rendering, not during
- Minimize work in effects - they can impact responsiveness
- Consider debouncing fast-changing signals:
Related
- use_signal - Create reactive state
- use_memo - Compute derived values
- use_future - Run async operations
- use_on_destroy - Cleanup on unmount