Quan Trinh’s Post

🚨 Angular Signals Pitfall: Why your effect() suddenly stops running I built a small StackBlitz demo today that shows a very tricky Angular Signals issue — and I think many Angular developers run into this without realizing why. The problem is simple: In Angular, effect() only tracks signals that are read during the execution of the effect. If you read a signal inside a conditional block, and that block doesn’t run the first time, Angular will not track the signal — and your effect will never run again when that signal changes. Example: ❌ Wrong way effect(() => { const button = this.buttonRef; if (button) { if (this.isReadOnly()) { button.disable(); } } }); If button is not available on the first run, isReadOnly() is never called → Angular does not track it → effect never re-runs. ✅ Correct way effect(() => { const readOnly = this.isReadOnly(); // read signal first const button = this.buttonRef; if (button && readOnly) { button.disable(); } }); Rule to remember: 👉 Always read signals at the top of effect() / computed() before any conditional logic or early return. One sentence that explains everything: No read = No track = No re-run This explains a lot of “randomly not updating UI” bugs when working with Angular Signals. I made a small demo showing: Wrong effect → button never disables Correct effect → button disables correctly If you're working with Angular Signals, this rule can save you hours of debugging. https://lnkd.in/g-qMit6V #Angular #AngularSignals #Frontend #WebDevelopment #ReactiveProgramming #JavaScript

  • text

I Think, most of this bugs, you mention comes from missunderstanting effect()/signals. Effect should be always the last choice. I see a lot of over using it.

Interesting. Why would you need to recalculate whole function in case all "if" values hadn't changed and anyway code execution will not hit line where signal value was read. Whole point of reactivity is to read what you need and when you need to reduce number of tracked values

This is described in the docs, how this is used is incorrect. A signal must always be called and should not be called conditionally.

See more comments

To view or add a comment, sign in

Explore content categories