<?xml version="1.0" encoding="UTF-8"?><rss xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:content="http://purl.org/rss/1.0/modules/content/" xmlns:atom="http://www.w3.org/2005/Atom" version="2.0" xmlns:cc="http://cyber.law.harvard.edu/rss/creativeCommonsRssModule.html">
    <channel>
        <title><![CDATA[Stories by Shambhvi on Medium]]></title>
        <description><![CDATA[Stories by Shambhvi on Medium]]></description>
        <link>https://medium.com/@shambhvi46?source=rss-f1457cdd0e1------2</link>
        <image>
            <url>https://cdn-images-1.medium.com/fit/c/150/150/1*GZ_P65MOtYO_NrJQyjRXKg.jpeg</url>
            <title>Stories by Shambhvi on Medium</title>
            <link>https://medium.com/@shambhvi46?source=rss-f1457cdd0e1------2</link>
        </image>
        <generator>Medium</generator>
        <lastBuildDate>Sun, 06 Dec 2026 01:02:24 GMT</lastBuildDate>
        <atom:link href="https://medium.com/@shambhvi46/feed" rel="self" type="application/rss+xml"/>
        <webMaster><![CDATA[yourfriends@medium.com]]></webMaster>
        <atom:link href="http://medium.superfeedr.com/" rel="hub"/>
        <item>
            <title><![CDATA[Tired of Angular List Issues? Own Immutability & Change Detection!]]></title>
            <link>https://medium.com/@shambhvi46/tired-of-angular-list-issues-own-immutability-change-detection-d8fd18644801?source=rss-f1457cdd0e1------2</link>
            <guid isPermaLink="false">https://medium.com/p/d8fd18644801</guid>
            <category><![CDATA[angular]]></category>
            <category><![CDATA[typescript]]></category>
            <dc:creator><![CDATA[Shambhvi]]></dc:creator>
            <pubDate>Tue, 03 Dec 2024 14:14:23 GMT</pubDate>
            <atom:updated>2024-12-03T14:14:23.549Z</atom:updated>
            <content:encoded><![CDATA[<p>Hey there! If you’re working with Angular and TypeScript, you’re likely dealing with lists or arrays a lot — whether it’s for displaying data in tables, forms, or any other dynamic UI. But here’s the thing: managing how you update those lists is a bit tricky, especially when it comes to <strong>change detection</strong> in Angular.</p><p>Let’s walk through a common scenario: you have a list of items, and you want to modify it by appending some options to each item. But… how you handle that list update in your code will determine whether Angular catches the change and updates the UI.</p><p>Curious? Let’s dive in!</p><h3>The Scenario</h3><p>Imagine you have a TestListInterface that looks like this:</p><pre>interface TestListInterface {<br>  part1?: Item[];<br>  part2?: Item[];<br>}<br><br>interface Item {<br>  name: string;<br>  threeDotOptions?: string[];<br>}</pre><p>You’ve got two parts — part1 and part2—which are arrays of Item objects. Each Item has a name and, optionally, a threeDotOptions array. Your goal is to add these options to the items in both parts.</p><h3>Let’s Look at Two Approaches</h3><h4>Approach 1: Using a Shallow Copy (This One Works!)</h4><p>Here’s the first way you might approach this:</p><pre>appendList(testList: TestListInterface): TestListInterface {<br>    const newList = { ...testList };  // Create a shallow copy<br>    newList?.part1?.forEach((item) =&gt; {<br>        item.threeDotOptions = this.appendDropDown(item, true); // Add options for part1<br>    });<br>    newList?.part2?.forEach((item) =&gt; {<br>        item.threeDotOptions = this.appendDropDown(item, false); // Add options for part2<br>    });<br>    return newList;<br>}</pre><p>So what’s happening here? Well, you’re creating a <strong>shallow copy</strong> of the testList with the spread operator ({...testList}). Then, you&#39;re updating the threeDotOptions of each item in both part1 and part2.</p><p><strong>Why does this work?</strong></p><ul><li>By creating a <strong>new reference</strong> for newList, Angular sees the change because the list object itself is a new one. Angular&#39;s <strong>change detection</strong> relies on detecting reference changes. When it sees that newList is a new reference, it knows something changed and updates the view accordingly.</li></ul><h4>Approach 2: Directly Mutating the List (Oops, Doesn’t Work!)</h4><p>Now, let’s look at another version, where we just mutate the testList directly:</p><pre>appendList(testList: TestListInterface): TestListInterface {<br>    testList?.part1?.forEach((item) =&gt; {<br>        item.threeDotOptions = this.appendDropDown(item, true); // Add options for part1<br>    });<br>    testList?.part2?.forEach((item) =&gt; {<br>        item.threeDotOptions = this.appendDropDown(item, false); // Add options for part2<br>    });<br>    return testList;<br>}</pre><p>In this version, you’re directly modifying the testList. You&#39;re not creating a new reference; instead, you’re modifying the existing one.</p><p><strong>Why doesn’t this work?</strong></p><ul><li>Here’s the tricky part: Angular doesn’t detect the change because <strong>the reference to </strong><strong>testList hasn’t changed</strong>. Angular checks if the reference has changed to decide whether it needs to update the UI. Since you&#39;re mutating the existing object, Angular doesn&#39;t see a reference change and therefore might <strong>skip updating the view</strong>.</li></ul><h3>The Key Difference: Immutability vs. Mutation</h3><p>This is where <strong>immutability</strong> comes in. When we <strong>don’t mutate</strong> the original list but create a new one (like in Approach 1), we ensure that Angular’s change detection can <strong>catch the update</strong>. In contrast, direct mutation (like in Approach 2) leaves the reference the same, so Angular can miss it.</p><h3>The Improved Solution: Combining Immutability and Deep Updates</h3><p>So, how do we fix this problem? The best solution is to <strong>not mutate</strong> anything and instead use methods like map() to create new lists. Let’s make the code even more robust:</p><pre>appendList(testList: TestListInterface): TestListInterface {<br>    const newList: TestListInterface = {<br>        ...testList,  // Shallow copy of testList<br>        part1: testList?.part1?.map((item) =&gt; ({<br>            ...item,  // Create a new item object<br>            threeDotOptions: this.appendDropDown(item, true), // Update part1 items<br>        })),<br>        part2: testList?.part2?.map((item) =&gt; ({<br>            ...item,  // Create a new item object<br>            threeDotOptions: this.appendDropDown(item, false), // Update part2 items<br>        }))<br>    };<br>    return newList;<br>}</pre><p>Here’s what changed:</p><ol><li><strong>Immutability</strong>: We’re creating a new newList, but we’re also using map() to create <strong>new item objects</strong> for each item in part1 and part2. This ensures that each item is updated immutably and that Angular will <strong>catch the change</strong>.</li><li><strong>Deep Updates</strong>: By using map(), we ensure that not only the list reference changes, but also each item inside the list is a <strong>new object</strong>. This ensures that <strong>all changes are properly detected</strong> by Angular’s change detection.</li></ol><h3>Why This Works</h3><ol><li><strong>Change Detection</strong>: Since both newList and the individual items are new references, Angular&#39;s change detection is able to detect the updates at both the list and item levels.</li><li><strong>No Side Effects</strong>: By keeping things immutable, you avoid side effects where the original data might be unintentionally altered elsewhere in your app.</li></ol><h3>Wrapping Up: Best Practices for Angular Lists</h3><p>Here are the takeaways:</p><ol><li><strong>Use Immutability</strong>: When updating lists, always aim for <strong>immutability</strong>. This involves creating new objects and arrays rather than mutating the existing ones. It’s cleaner and ensures proper change detection.</li><li><strong>Avoid Direct Mutation</strong>: Direct mutation of lists (or any objects) can cause Angular’s change detection to miss updates, leading to UI inconsistencies.</li><li><strong>Use </strong><strong>map() for Deep Updates</strong>: When updating lists, especially complex ones, use map() to transform the list and its items. This ensures that Angular sees the changes at all levels.</li></ol><p>By following these simple practices, you can make sure that your Angular app is both performant and responsive, with smooth, predictable UI updates.</p><h4>If this was useful to you, you can support me :</h4><figure><a href="https://buymeacoffee.com/shambhvi"><img alt="" src="https://cdn-images-1.medium.com/max/480/1*kTCDr8mtTaQjG5vCceU3WA.gif" /></a></figure><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=d8fd18644801" width="1" height="1" alt="">]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[An alternative of setTimeOut in angular]]></title>
            <link>https://medium.com/@shambhvi46/an-alternative-of-settimeout-in-angular-0cba5918e650?source=rss-f1457cdd0e1------2</link>
            <guid isPermaLink="false">https://medium.com/p/0cba5918e650</guid>
            <dc:creator><![CDATA[Shambhvi]]></dc:creator>
            <pubDate>Sat, 02 Mar 2024 20:49:02 GMT</pubDate>
            <atom:updated>2024-03-02T20:49:02.319Z</atom:updated>
            <content:encoded><![CDATA[<p>In this post, I am going to discuss the alternative of setTimeOut which is not the best practice.</p><p>So, I encountered an issue where I had to use this because I had to change the styling of an HTML element which was rendered after the response was fetched. Previous code-</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/764/1*eH5A6iwI8m_bfcF4bdh87Q.png" /></figure><p>So I resolved it using Subject and delay in subscription:</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/752/1*ac372evOF7afTqgCvdGIaw.png" /></figure><p>If this was useful to you, you can support me :</p><figure><a href="https://www.buymeacoffee.com/shambhvi"><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*QCQqlZr6doDP-cszzpaSpw.png" /></a></figure><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=0cba5918e650" width="1" height="1" alt="">]]></content:encoded>
        </item>
    </channel>
</rss>