Reputation: 15271
we have:
effect(() => {
console.log(`Value is ${newValue()}`);
});
We don't want whole console.log
to run on initial signal set. We want it to run only starting with 2nd change of the signal.
Should we have a private field initialRun
(true
initially) in our component and set it to false
at the end of our effect()
code so that each subsequent run actually executes !initialRun && console.log(....)
?
Or is there any smarter way?
Upvotes: 7
Views: 2469
Reputation: 21048
According to https://github.com/angular/angular/issues/54372 there does not seem to be a built-in way to ignore the first run. But you should not need a private field either. A local variable should suffice (closures FTW):
constructor() {
let initialRun = true;
effect(() => {
// Also read the signal the first time to setup the reactive dependency
const value = newValue();
if (!initialRun) {
console.log(`Value is ${value}`);
}
initialRun = false;
});
}
Upvotes: 1