explicitEffect
Created by Matthieu Riegler Enea Jahollari
explicitEffect
is a helper function that allows you to create an effect that only depends on the provided signals in the deps array.
It will run the effect function only when the signals in the deps array change.
Think about this pattern:
This pattern is very common in Angular apps, and it’s used to prevent unnecessary signal reads or writes when the effect function is called.
explicitEffect
is a helper function that does exactly this, but in a more explicit way.
Usage
explicitEffect
accepts an array of signals and a function that will be called when the signals change.
The deps array accepts:
- Signals (also computed signals)
- Writable signals
- Functions that have signal reads as dependencies (ex:
() => this.count()
)
Cleanup
An optional second argument can be provided to the explicitEffect
function that will be called when the effect is cleaned up.
Defer
In addition to the regular effect
options, you can also pass a defer
value as part of the third object argument of explicitEffect
. This allows the computation not to execute immediately and only run on first deps change.