Angular

angular/computed-not-effect

Derive state with computed() (or linkedSignal()); never copy one signal into another inside effect().

umiarkowanaręcznefe-angular

Dlaczego

effect(() => this.total.set(this.price() * this.qty())) runs after the change, so the template first renders the old total and then needs another pass; chains of such effects cause extra change detection, ExpressionChangedAfterItHasBeenChecked errors or loops. Angular's guide calls effects a last resort for syncing with the outside world.

Poprawka

total = computed(() => this.price() * this.qty()). When the derived value must also be settable by the user (a selection reset when the list changes), use linkedSignal(). Keep effect() for logging, localStorage, a canvas or a third-party widget. This rule is manual because the engine does not read .ts files; the angular-eslint rules and a review of every effect( that calls .set( or .update( find it.

Przykłady

// bad
total = signal(0)
constructor() { effect(() => this.total.set(this.price() * this.qty())) }
// good
total = computed(() => this.price() * this.qty())

Źródła

Fakty

Identyfikator
angular/computed-not-effect
Waga
umiarkowana
Sprawdzanie
ręczne ocenia człowiek; raport podaje to jako listę kontrolną
Dotyczy plików
angular
Tylko w projektach z
angular
Uczy jej skill
fe-angular Angular components
Od wersji
0.1.0

Plik reguły na GitHubie

Przeczytaj w terminalu

npx github:dawidolko/frontend-skills explain angular/computed-not-effect