<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom" xmlns:dc="http://purl.org/dc/elements/1.1/">
  <channel>
    <title>DEV Community: Ahmed Khan</title>
    <description>The latest articles on DEV Community by Ahmed Khan (@learn_with_ahmed).</description>
    <link>https://dev.to/learn_with_ahmed</link>
    <image>
      <url>https://media2.dev.to/dynamic/image/width=90,height=90,fit=cover,gravity=auto,format=auto/https:%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Fuser%2Fprofile_image%2F3382353%2Feea672cf-e1f3-4b0c-a628-6f1c8a16e4a0.jpg</url>
      <title>DEV Community: Ahmed Khan</title>
      <link>https://dev.to/learn_with_ahmed</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://dev.to/feed/learn_with_ahmed"/>
    <language>en</language>
    <item>
      <title>When to Skip effect() in Angular and What You Should Use Instead</title>
      <dc:creator>Ahmed Khan</dc:creator>
      <pubDate>Wed, 23 Jul 2025 15:01:23 +0000</pubDate>
      <link>https://dev.to/learn_with_ahmed/when-to-skip-effect-in-angular-and-what-you-should-use-instead-c2</link>
      <guid>https://dev.to/learn_with_ahmed/when-to-skip-effect-in-angular-and-what-you-should-use-instead-c2</guid>
      <description>&lt;p&gt;Angular’s effect() function is a powerful tool — but like any powerful tool, it can be misused. One key rule to remember is this:&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;Only use effects when you need to do something outside the template that can’t be done with standard data binding.&lt;/p&gt;
&lt;/blockquote&gt;

&lt;h2&gt;
  
  
  Keep Angular’s Auto-Tracking in Mind
&lt;/h2&gt;

&lt;p&gt;One of the important things to understand about effect() is that Angular uses implicit tracking — meaning signals accessed within an effect are automatically tracked, even if they’re buried inside called methods.&lt;/p&gt;

&lt;p&gt;Take this example:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;effect(() =&amp;gt; {
  this.logError();
});

logError(): void {
  const error = this.error();
  if (error) {
    console.error(error);
  }
}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Even though error() isn’t directly referenced in the effect callback, Angular tracks it because it’s used inside logError().&lt;/p&gt;

&lt;p&gt;This behavior shows how tightly coupled effects are to rendering. Any signals accessed during rendering (directly or indirectly) are tracked, and when they change, Angular re-runs the effect.&lt;/p&gt;

&lt;h2&gt;
  
  
  🚫 When Not to Use Effects
&lt;/h2&gt;

&lt;p&gt;Angular’s own documentation clearly advises against using effects to propagate state. Here’s why:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;You can easily end up in circular update situations.&lt;/li&gt;
&lt;li&gt;Auto-tracking might lead to unpredictable side effects that are hard to debug.&lt;/li&gt;
&lt;li&gt;Effects lean toward imperative code, which undermines the declarative style of reactive programming.&lt;/li&gt;
&lt;li&gt;In general, if you’re using effects to update signals or drive state logic, you’re probably reaching for the wrong tool.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Also, keep in mind: Signals are glitch-free. If you update a signal multiple times in one synchronous execution, only the last update is used. This is great for rendering — but terrible for representing real-time events or sequential actions.&lt;/p&gt;

&lt;h2&gt;
  
  
  ✅ Legit Uses of effect()
&lt;/h2&gt;

&lt;p&gt;Besides rendering-related use cases, effects also power reactive helpers behind the scenes.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;For example:&lt;/strong&gt;&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;toObservable() in Angular&lt;/li&gt;
&lt;li&gt;rxMethod() in the NgRx Signal Store&lt;/li&gt;
&lt;li&gt;Utilities from libraries like ngxtension by Chau Tran and Enea Jahollari&lt;/li&gt;
&lt;li&gt;These tools use effect() under the hood to bridge signals with other reactive systems, especially RxJS.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;So, How Should You React to Signal Changes?&lt;br&gt;
When you want to perform actions based on changes in signals, here are some clean alternatives:&lt;/p&gt;
&lt;h2&gt;
  
  
  1. Use computed()
&lt;/h2&gt;

&lt;p&gt;If you can derive the new value from other signals synchronously — this is the most declarative and idiomatic approach.&lt;/p&gt;
&lt;h2&gt;
  
  
  2. React to Events, Not Signals
&lt;/h2&gt;

&lt;p&gt;Instead of reacting to a signal change, respond to the event that caused the change. This avoids weird auto-tracking issues.&lt;/p&gt;
&lt;h2&gt;
  
  
  3. Leverage RxJS
&lt;/h2&gt;

&lt;p&gt;In many cases, RxJS is a better fit for complex reactivity. You can convert signals to observables (and back) using toObservable() and toSignal(). RxJS’s operators like switchMap() are also great for handling things like overlapping HTTP requests without race conditions.&lt;/p&gt;
&lt;h2&gt;
  
  
  4. Use Reactive Helpers
&lt;/h2&gt;

&lt;p&gt;If you’re using Signal Stores or reactive libraries, take advantage of utilities like rxMethod() or deriveAsync().&lt;/p&gt;

&lt;p&gt;Example: rxMethod in Signal Store&lt;br&gt;
Here’s a minimal example of how you can use rxMethod() to load data reactively:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;export const DessertDetailStore = signalStore(
  { providedIn: 'root' },
  withState({
    dessert: initDessert,
    loading: false,
  }),
  withMethods((store, dessertService = inject(DessertService)) =&amp;gt; ({
    rxLoad: rxMethod&amp;lt;number&amp;gt;(
      pipe(
        tap(() =&amp;gt; patchState(store, { loading: true })),
        switchMap((id) =&amp;gt; dessertService.findById(id)),
        tap((dessert) =&amp;gt; patchState(store, { dessert, loading: false }))
      )
    ),
  }))
);
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h2&gt;
  
  
  Final Thoughts
&lt;/h2&gt;

&lt;p&gt;Think of effect() as a last resort: use it only when you can’t achieve your goal with bindings, computed, or reactive patterns. Misusing it can make your code harder to reason about and more prone to bugs.&lt;/p&gt;

&lt;p&gt;Instead, lean into RxJS, computed signals, and event-based logic for most scenarios. Tools like rxMethod() offer a sweet spot between Signals and RxJS, giving you the best of both worlds.&lt;/p&gt;

&lt;p&gt;Let me know if you’d like help converting existing effect-heavy code into a cleaner reactive approach!&lt;/p&gt;

</description>
      <category>webdev</category>
      <category>programming</category>
      <category>angular</category>
      <category>ui</category>
    </item>
    <item>
      <title>Understanding linkedSignal vs computed in Angular Signals</title>
      <dc:creator>Ahmed Khan</dc:creator>
      <pubDate>Wed, 23 Jul 2025 14:56:12 +0000</pubDate>
      <link>https://dev.to/learn_with_ahmed/understanding-linkedsignal-vs-computed-in-angular-signals-aca</link>
      <guid>https://dev.to/learn_with_ahmed/understanding-linkedsignal-vs-computed-in-angular-signals-aca</guid>
      <description>&lt;p&gt;While exploring Angular’s Signals system, I came across something that initially felt a bit redundant — linkedSignal. At first glance, it seems like you could just use computed() to handle state that depends on other signals. For example:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;const shippingOptions = signal(['Ground', 'Air', 'Sea']);
const selectedOption = computed(() =&amp;gt; shippingOptions()[0]);
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;This works fine if all you need is a reactive read of the first shipping option. But here’s where linkedSignal offers more power.&lt;/p&gt;

&lt;h3&gt;
  
  
  The Key Difference: Writability
&lt;/h3&gt;

&lt;p&gt;What sets linkedSignal apart is that it’s writable, whereas computed() is read-only. That means with linkedSignal, you can derive state reactively like computed, and update it locally when needed.&lt;/p&gt;

&lt;p&gt;For example:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;const selectedOption = linkedSignal(() =&amp;gt; shippingOptions()[0]);
selectedOption.set('Air'); // You can override it manually
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;This is useful when you want something to default to a derived value, but still let the user (or component logic) update it manually afterward.&lt;/p&gt;

&lt;h3&gt;
  
  
  Handling Previous State
&lt;/h3&gt;

&lt;p&gt;Another benefit of linkedSignal is access to the previous value during computation. You can do things like:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;const selectedOption = linkedSignal({
  source: shippingOptions,
  compute: (newValue, oldValue) =&amp;gt; {
    // maybe preserve selection if still valid
    return newValue.includes(oldValue) ? oldValue : newValue[0];
  }
});
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;This kind of logic isn’t possible with computed(), which doesn’t track previous values in the same way.&lt;/p&gt;

&lt;h3&gt;
  
  
  When to Prefer linkedSignal
&lt;/h3&gt;

&lt;p&gt;Use linkedSignal when:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;You want a derived default value, but still need the flexibility to update it locally.&lt;/li&gt;
&lt;li&gt;You’re working with inputs or external signals but want to maintain a local copy that can be overridden internally.&lt;/li&gt;
&lt;li&gt;You need access to the previous state during recalculations.&lt;/li&gt;
&lt;li&gt;In contrast, computed() is perfect when your signal is purely reactive and read-only, with no need to write back or override.&lt;/li&gt;
&lt;/ul&gt;

&lt;h3&gt;
  
  
  TL;DR
&lt;/h3&gt;

&lt;p&gt;computed = readonly reactive value&lt;br&gt;
linkedSignal = reactive default + writable override + previous value access&lt;/p&gt;

&lt;p&gt;Use linkedSignal when you want the best of both worlds — reactive derivation and local control.&lt;/p&gt;

&lt;p&gt;Let me know if you want to see a working example in a real component!&lt;/p&gt;

</description>
      <category>angular</category>
      <category>webdev</category>
      <category>ui</category>
      <category>programming</category>
    </item>
  </channel>
</rss>
