Freya uses
use_side_effect instead of use_effect to be explicit about side-effect behavior.Function Signature
Parameters
A mutable closure that will be called whenever any reactive values read inside it change. The closure is also called once immediately when the component mounts.
Basic Usage
How It Works
The side effect automatically subscribes to anyState values that are read inside it:
- The callback runs once when the component first renders
- Any
Statevalues accessed via.read()are tracked - When those values change, the callback runs again
- The subscription is automatically cleaned up when the component unmounts
Variants
use_side_effect - Standard side effect
Runs synchronously when dependencies change:
use_after_side_effect - Deferred execution
Runs after the current frame completes, useful for operations that should happen after rendering:
use_side_effect_value - Side effect with return value
Runs a side effect and returns a reactive State with the result:
use_side_effect_with_deps - Manual dependencies
Specify explicit dependencies instead of automatic tracking:
Examples
Logging state changes
Multiple reactive values
Conditional effects
Async operations
When to Use
Useuse_side_effect for:
- Logging and debugging
- Syncing with external systems
- Triggering async operations
- Updating DOM or platform APIs
- Performing cleanup or initialization
- Deriving values from state (use
use_memoinstead) - Simple state transformations (compute directly in render)
Comparison with use_memo
| Feature | use_side_effect | use_memo |
|---|---|---|
| Purpose | Side effects | Cached computations |
| Returns value | No | Yes |
| Use case | I/O, logging, async | Expensive calculations |
| Re-runs | On dependency change | On dependency change |
Performance Considerations
- Side effects run synchronously by default - use
use_after_side_effectfor expensive operations - Avoid reading too many reactive values in a single effect
- Keep side effects focused and specific
- Use
peek()instead ofread()if you don’t want to subscribe to changes