<?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: Madhu Sudhanan P</title>
    <description>The latest articles on DEV Community by Madhu Sudhanan P (@madhust).</description>
    <link>https://dev.to/madhust</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%2F304089%2Fccf39951-bd70-4676-9e68-2d4ba65f688c.jpeg</url>
      <title>DEV Community: Madhu Sudhanan P</title>
      <link>https://dev.to/madhust</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://dev.to/feed/madhust"/>
    <language>en</language>
    <item>
      <title>Angular Dependency Injection — Inject service inside custom Rxjs operators</title>
      <dc:creator>Madhu Sudhanan P</dc:creator>
      <pubDate>Sun, 24 Nov 2024 17:18:56 +0000</pubDate>
      <link>https://dev.to/madhust/angular-dependency-injection-inject-service-inside-custom-rxjs-operators-1cfp</link>
      <guid>https://dev.to/madhust/angular-dependency-injection-inject-service-inside-custom-rxjs-operators-1cfp</guid>
      <description>&lt;p&gt;Angular is an opiniated framework. It’s really an excitement to see how its diverging towards developer flexibility and becoming stronger day by day. I believe it’s a happy era for the Angular lovers.&lt;/p&gt;

&lt;p&gt;Dependency injection and RxJS — In my opinion, are powerful features which made Angular unique and flexible. RxJS custom operators are a great way to compose complex logic into reusable functions.&lt;/p&gt;

&lt;p&gt;Recently I came across a usecase where I need to create a custom RxJS operator which uses service to achieve some complex usecase.&lt;/p&gt;

&lt;p&gt;You can use &lt;code&gt;inject&lt;/code&gt; method to access desired service instance, inject must be used within &lt;a href="https://angular.dev/guide/di/dependency-injection-context" rel="noopener noreferrer"&gt;dependency injection context&lt;/a&gt; though. To achieve this, Rxjs operator can be registered as a &lt;code&gt;InjectionToken&lt;/code&gt; which uses &lt;code&gt;useFactory&lt;/code&gt; function to instantiate its value. Since &lt;code&gt;useFactory&lt;/code&gt; is a injection context, the services can be injected without any problem using the inject method.&lt;/p&gt;

&lt;p&gt;Let’s see how the below service can be injected in a rxjs custom operator.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight typescript"&gt;&lt;code&gt;&lt;span class="c1"&gt;// Service&lt;/span&gt;
&lt;span class="p"&gt;@&lt;/span&gt;&lt;span class="nd"&gt;Injectable&lt;/span&gt;&lt;span class="p"&gt;({&lt;/span&gt;
  &lt;span class="na"&gt;providedIn&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;root&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
&lt;span class="p"&gt;})&lt;/span&gt;
&lt;span class="k"&gt;export&lt;/span&gt; &lt;span class="kd"&gt;class&lt;/span&gt; &lt;span class="nc"&gt;Multiplier&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="k"&gt;public&lt;/span&gt; &lt;span class="nf"&gt;transfrom&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;val&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="kr"&gt;number&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="nx"&gt;val&lt;/span&gt; &lt;span class="o"&gt;*&lt;/span&gt; &lt;span class="mi"&gt;2&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
  &lt;span class="p"&gt;}&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;The below custom RxJS operator simply multiple the given value with 2 using the Multiplier service.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight typescript"&gt;&lt;code&gt;&lt;span class="k"&gt;import&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="nx"&gt;inject&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;InjectionToken&lt;/span&gt; &lt;span class="p"&gt;}&lt;/span&gt; &lt;span class="k"&gt;from&lt;/span&gt; &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;@angular/core&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="k"&gt;import&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="nx"&gt;Multiplier&lt;/span&gt; &lt;span class="p"&gt;}&lt;/span&gt; &lt;span class="k"&gt;from&lt;/span&gt; &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;../services/multiplier&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="k"&gt;import&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="nx"&gt;map&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;OperatorFunction&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;pipe&lt;/span&gt; &lt;span class="p"&gt;}&lt;/span&gt; &lt;span class="k"&gt;from&lt;/span&gt; &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;rxjs&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;

&lt;span class="k"&gt;export&lt;/span&gt; &lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;MULTIPLIER_OPERATOR&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nx"&gt;InjectionToken&lt;/span&gt;&lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="nx"&gt;OperatorFunction&lt;/span&gt;&lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="kr"&gt;number&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="kr"&gt;number&lt;/span&gt;&lt;span class="o"&gt;&amp;gt;&amp;gt;&lt;/span&gt;
  &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="k"&gt;new&lt;/span&gt; &lt;span class="nc"&gt;InjectionToken&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;Multipler operator&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="nf"&gt;factory&lt;/span&gt;&lt;span class="p"&gt;():&lt;/span&gt; &lt;span class="nx"&gt;OperatorFunction&lt;/span&gt;&lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="kr"&gt;number&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="kr"&gt;number&lt;/span&gt;&lt;span class="o"&gt;&amp;gt;&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
      &lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;multipler&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nf"&gt;inject&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;Multiplier&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt; &lt;span class="c1"&gt;// injected Multipler service&lt;/span&gt;
      &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="nf"&gt;pipe&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nf"&gt;map&lt;/span&gt;&lt;span class="p"&gt;((&lt;/span&gt;&lt;span class="na"&gt;x&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="kr"&gt;number&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="nx"&gt;multipler&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;transfrom&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;x&lt;/span&gt;&lt;span class="p"&gt;)));&lt;/span&gt;
    &lt;span class="p"&gt;},&lt;/span&gt;
  &lt;span class="p"&gt;});&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Now the custom operator can be used within the components using the &lt;code&gt;@Inject&lt;/code&gt; decorator.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight typescript"&gt;&lt;code&gt;&lt;span class="k"&gt;import&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="nx"&gt;Component&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;Inject&lt;/span&gt; &lt;span class="p"&gt;}&lt;/span&gt; &lt;span class="k"&gt;from&lt;/span&gt; &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;@angular/core&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="k"&gt;import&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="nx"&gt;BehaviorSubject&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;Observable&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="k"&gt;of&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;OperatorFunction&lt;/span&gt; &lt;span class="p"&gt;}&lt;/span&gt; &lt;span class="k"&gt;from&lt;/span&gt; &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;rxjs&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="k"&gt;import&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="nx"&gt;MULTIPLIER_OPERATOR&lt;/span&gt; &lt;span class="p"&gt;}&lt;/span&gt; &lt;span class="k"&gt;from&lt;/span&gt; &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;./rxjs-operators/custom&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="k"&gt;import&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="nx"&gt;CommonModule&lt;/span&gt; &lt;span class="p"&gt;}&lt;/span&gt; &lt;span class="k"&gt;from&lt;/span&gt; &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;@angular/common&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;

&lt;span class="k"&gt;export&lt;/span&gt; &lt;span class="kd"&gt;class&lt;/span&gt; &lt;span class="nc"&gt;App&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="k"&gt;private&lt;/span&gt; &lt;span class="nx"&gt;_count&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="mi"&gt;1&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
  &lt;span class="k"&gt;public&lt;/span&gt; &lt;span class="nx"&gt;base&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="k"&gt;new&lt;/span&gt; &lt;span class="nc"&gt;BehaviorSubject&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="k"&gt;this&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;_count&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
  &lt;span class="k"&gt;public&lt;/span&gt; &lt;span class="nx"&gt;val&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="k"&gt;new&lt;/span&gt; &lt;span class="nc"&gt;Observable&lt;/span&gt;&lt;span class="p"&gt;();&lt;/span&gt;

  &lt;span class="nf"&gt;constructor&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;
    &lt;span class="p"&gt;@&lt;/span&gt;&lt;span class="nd"&gt;Inject&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;MULTIPLIER_OPERATOR&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="c1"&gt;// injected custom operator&lt;/span&gt;
    &lt;span class="k"&gt;private&lt;/span&gt; &lt;span class="nx"&gt;multiplier&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nx"&gt;OperatorFunction&lt;/span&gt;&lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="kr"&gt;number&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="kr"&gt;number&lt;/span&gt;&lt;span class="o"&gt;&amp;gt;&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
      &lt;span class="k"&gt;this&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;val&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="k"&gt;this&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;base&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;pipe&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="k"&gt;this&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;multiplier&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt; &lt;span class="c1"&gt;// used rxjs operator&lt;/span&gt;
  &lt;span class="p"&gt;}&lt;/span&gt;

  &lt;span class="p"&gt;.....&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fp3y3yjos4dv2qpgoqmj7.gif" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fp3y3yjos4dv2qpgoqmj7.gif" alt="Image description" width="427" height="308"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;There are several other ways to achieve this same usecase like creating a separate service and a method that would return custom operator. But I like InjectionToken approach due to its self containability. RxJS isn’t going anywhere in near future even with introduction of signals so it’s worth experimiting on it.&lt;/p&gt;

&lt;p&gt;You can see the full working example here.&lt;br&gt;
&lt;iframe src="https://stackblitz.com/edit/angular-ry49oh?embed=1&amp;amp;file=src%2Fmain.ts" width="100%" height="500"&gt;
&lt;/iframe&gt;
&lt;/p&gt;

&lt;p&gt;Happy Coding…🎉&lt;/p&gt;

</description>
      <category>angular</category>
      <category>javascript</category>
      <category>rxjs</category>
      <category>webdev</category>
    </item>
    <item>
      <title>Angular — Deferred Loading using defer block — What you need to know</title>
      <dc:creator>Madhu Sudhanan P</dc:creator>
      <pubDate>Sun, 01 Oct 2023 18:13:04 +0000</pubDate>
      <link>https://dev.to/madhust/angular-deferred-loading-using-defer-block-what-you-need-to-know-49b9</link>
      <guid>https://dev.to/madhust/angular-deferred-loading-using-defer-block-what-you-need-to-know-49b9</guid>
      <description>&lt;p&gt;Lazy loading in Angular is an important feature that every developer should know. It helps load components/parts/code on demand and improves the UX by loading what is needed at the moment.&lt;/p&gt;

&lt;p&gt;Angular Lazy loading was purely routing-based and to load non-route modules you need to use special code which involves &lt;code&gt;import&lt;/code&gt;and &lt;code&gt;ngComponentOutlet&lt;/code&gt;. Last year, I faced a similar challenge and achieved this in a possible declarative way. You can see this in the below post.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://dev.to/madhust/lazy-loading-non-routable-angular-modules-imperative-declarative-pattern-3a33"&gt;https://dev.to/madhust/lazy-loading-non-routable-angular-modules-imperative-declarative-pattern-3a33&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;The previous solution I used has some pitfalls and it’s not a 100% declarative way. But with the new &lt;code&gt;@defer&lt;/code&gt; block syntax and standalone component, you can do the deferred loading in a much simpler and more declarative way.&lt;/p&gt;

&lt;p&gt;Let’s see what’s new with the defer block.&lt;/p&gt;

&lt;h2&gt;
  
  
  Basic defer block
&lt;/h2&gt;

&lt;p&gt;A basic defer block initialization will look like below. It does contain any condition so basically the content will be loaded immediately. In the following section, let’s see some complex examples using the defer block.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight xml"&gt;&lt;code&gt;@defer {
  &lt;span class="nt"&gt;&amp;lt;app-defer&amp;gt;&amp;lt;/app-defer&amp;gt;&lt;/span&gt;
}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;So how does the defer block lazy load its content? Basically during the compilation, the component(s) and its dependencies(directives, pipes, etc.) will be packed into a separate chunk and will loaded when the defer block conditions are met. You can see that in the below image.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fvk1aqemv06cigsrikmwn.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fvk1aqemv06cigsrikmwn.png" alt="Lazy Load - Defer block"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fq94n3eydblsrrxfnt2nu.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fq94n3eydblsrrxfnt2nu.png" alt="Defer block - Lazy load chunk"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;h2&gt;
  
  
  Placeholder block
&lt;/h2&gt;

&lt;p&gt;As the name implies, the &lt;code&gt;@placeholder&lt;/code&gt; block will act as the placeholder for the content that will be defer loaded. This is an optional block.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight xml"&gt;&lt;code&gt;&lt;span class="nt"&gt;&amp;lt;input&lt;/span&gt; &lt;span class="na"&gt;type=&lt;/span&gt;&lt;span class="s"&gt;"checkbox"&lt;/span&gt; &lt;span class="err"&gt;#check&lt;/span&gt;&lt;span class="nt"&gt;/&amp;gt;&lt;/span&gt;

@defer (on interaction(check)) {
  &lt;span class="nt"&gt;&amp;lt;app-defer&amp;gt;&amp;lt;/app-defer&amp;gt;&lt;/span&gt;
} @placeholder {
  &lt;span class="nt"&gt;&amp;lt;span&amp;gt;&lt;/span&gt;Loading&lt;span class="nt"&gt;&amp;lt;/span&amp;gt;&lt;/span&gt;
}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;You can mention the minimum time to show the placeholder block before swapping to the next component by using the &lt;code&gt;minimum&lt;/code&gt; parameter.&lt;/p&gt;

&lt;p&gt;In addition to the placeholder purpose, you might need to use a placeholder if you want to use a zero parameter &lt;code&gt;interaction&lt;/code&gt;, &lt;code&gt;hover&lt;/code&gt;, or &lt;code&gt;viewport&lt;/code&gt; triggers. Now the placeholder block will act as a target for these triggers.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight xml"&gt;&lt;code&gt;@defer (on interaction) {
  &lt;span class="nt"&gt;&amp;lt;app-defer&amp;gt;&amp;lt;/app-defer&amp;gt;&lt;/span&gt;
} @placeholder {
  &lt;span class="nt"&gt;&amp;lt;span&amp;gt;&lt;/span&gt;Loading&lt;span class="nt"&gt;&amp;lt;/span&amp;gt;&lt;/span&gt;
}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;blockquote&gt;
&lt;p&gt;The content of the &lt;code&gt;@placeholder&lt;/code&gt;, &lt;code&gt;@loading&lt;/code&gt; and &lt;code&gt;@error&lt;/code&gt; are eagerly loaded.&lt;/p&gt;
&lt;/blockquote&gt;

&lt;h2&gt;
  
  
  Loading block
&lt;/h2&gt;

&lt;p&gt;The next optional block that allows you to have a smooth transition to the next component is the &lt;code&gt;@loading&lt;/code&gt; block.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight xml"&gt;&lt;code&gt;@defer (on interaction(check)) {
  &lt;span class="nt"&gt;&amp;lt;app-defer&amp;gt;&amp;lt;/app-defer&amp;gt;&lt;/span&gt;
} @placeholder {
  &lt;span class="nt"&gt;&amp;lt;span&amp;gt;&lt;/span&gt;Placeholder&lt;span class="nt"&gt;&amp;lt;/span&amp;gt;&lt;/span&gt;
} @loading {
  &lt;span class="nt"&gt;&amp;lt;span&amp;gt;&lt;/span&gt;Loading&lt;span class="nt"&gt;&amp;lt;/span&amp;gt;&lt;/span&gt;
}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;blockquote&gt;
&lt;p&gt;The content of the &lt;code&gt;@placeholder&lt;/code&gt;, &lt;code&gt;@loading&lt;/code&gt; and &lt;code&gt;@error&lt;/code&gt; are eagerly loaded.&lt;/p&gt;
&lt;/blockquote&gt;

&lt;h2&gt;
  
  
  Error block
&lt;/h2&gt;

&lt;p&gt;The &lt;code&gt;@error&lt;/code&gt; block will render its content when any error happens during the loading of the deferred content.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight xml"&gt;&lt;code&gt;&lt;span class="nt"&gt;&amp;lt;input&lt;/span&gt; &lt;span class="na"&gt;type=&lt;/span&gt;&lt;span class="s"&gt;"checkbox"&lt;/span&gt; &lt;span class="err"&gt;#check&lt;/span&gt;&lt;span class="nt"&gt;/&amp;gt;&lt;/span&gt;

@defer (on interaction(check)) {
  &lt;span class="nt"&gt;&amp;lt;app-defer&amp;gt;&amp;lt;/app-defer&amp;gt;&lt;/span&gt;
} @placeholder {
  &lt;span class="nt"&gt;&amp;lt;span&amp;gt;&lt;/span&gt;Placeholder&lt;span class="nt"&gt;&amp;lt;/span&amp;gt;&lt;/span&gt;
} @loading {
  &lt;span class="nt"&gt;&amp;lt;span&amp;gt;&lt;/span&gt;Loading&lt;span class="nt"&gt;&amp;lt;/span&amp;gt;&lt;/span&gt;
} @error {
  &lt;span class="nt"&gt;&amp;lt;span&amp;gt;&lt;/span&gt;Error Loading...&lt;span class="nt"&gt;&amp;lt;/span&amp;gt;&lt;/span&gt;
}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;blockquote&gt;
&lt;p&gt;The content of the &lt;code&gt;@placeholder&lt;/code&gt;, &lt;code&gt;@loading&lt;/code&gt; and &lt;code&gt;@error&lt;/code&gt; are eagerly loaded.&lt;/p&gt;
&lt;/blockquote&gt;

&lt;h2&gt;
  
  
  Deferred loading conditions using on, when, and prefetch
&lt;/h2&gt;

&lt;p&gt;I’d say that the Angular team did amazing work here by providing both declarative and imperative approaches to lazy load the content.&lt;/p&gt;

&lt;p&gt;The declarative conditions can be provided using the &lt;code&gt;on&lt;/code&gt; whereas the &lt;code&gt;where&lt;/code&gt; allows the developer to provide their own logic.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight xml"&gt;&lt;code&gt;&lt;span class="nt"&gt;&amp;lt;input&lt;/span&gt; &lt;span class="na"&gt;type=&lt;/span&gt;&lt;span class="s"&gt;"checkbox"&lt;/span&gt; &lt;span class="err"&gt;#check&lt;/span&gt;&lt;span class="nt"&gt;/&amp;gt;&lt;/span&gt;

@defer (on interaction(check)) { 
 &lt;span class="nt"&gt;&amp;lt;app-defer&amp;gt;&amp;lt;/app-defer&amp;gt;&lt;/span&gt;
}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;





&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight xml"&gt;&lt;code&gt;@defer (when showSignal()) { // showSignal is a signal.
  &lt;span class="nt"&gt;&amp;lt;app-defer&amp;gt;&amp;lt;/app-defer&amp;gt;&lt;/span&gt;
}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h2&gt;
  
  
  Using on (Declarative trigger conditions)
&lt;/h2&gt;

&lt;p&gt;You can use the following declarative trigger conditions to defer the load of your component.&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;&lt;code&gt;immediate&lt;/code&gt;&lt;/li&gt;
&lt;li&gt;&lt;code&gt;idle&lt;/code&gt;&lt;/li&gt;
&lt;li&gt;
&lt;code&gt;interaction&lt;/code&gt;/&lt;code&gt;hover&lt;/code&gt;
&lt;/li&gt;
&lt;li&gt;&lt;code&gt;viewport&lt;/code&gt;&lt;/li&gt;
&lt;li&gt;&lt;code&gt;timer&lt;/code&gt;&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;You can use multiple declarative triggers for a single defer block. All the trigger conditions are combined using &lt;code&gt;OR&lt;/code&gt; logic. For instance, the below code will load content either the button is clicked or the checkbox is hovered.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight xml"&gt;&lt;code&gt;&lt;span class="nt"&gt;&amp;lt;button&lt;/span&gt; &lt;span class="err"&gt;#button&lt;/span&gt;&lt;span class="nt"&gt;&amp;gt;&lt;/span&gt;Load Defer Component&lt;span class="nt"&gt;&amp;lt;/button&amp;gt;&lt;/span&gt;

&lt;span class="nt"&gt;&amp;lt;input&lt;/span&gt; &lt;span class="na"&gt;type=&lt;/span&gt;&lt;span class="s"&gt;"checkbox"&lt;/span&gt; &lt;span class="err"&gt;#check&lt;/span&gt;&lt;span class="nt"&gt;/&amp;gt;&lt;/span&gt;

@defer (on interaction(button), hover(check)) {
 &lt;span class="nt"&gt;&amp;lt;app-defer&amp;gt;&amp;lt;/app-defer&amp;gt;&lt;/span&gt;
} @placeholder {
  &lt;span class="nt"&gt;&amp;lt;span&amp;gt;&lt;/span&gt;Loading&lt;span class="nt"&gt;&amp;lt;/span&amp;gt;&lt;/span&gt;
}

&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h3&gt;
  
  
  immediate
&lt;/h3&gt;

&lt;p&gt;The &lt;code&gt;immediate&lt;/code&gt; condition will load the defer block content immediately once the browser is ready.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight xml"&gt;&lt;code&gt;@defer (on immediate) {
  &lt;span class="nt"&gt;&amp;lt;app-defer&amp;gt;&amp;lt;/app-defer&amp;gt;&lt;/span&gt;
}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h3&gt;
  
  
  idle
&lt;/h3&gt;

&lt;p&gt;This is the default behavior of the defer block and would render the content when the browser is in an idle state.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight xml"&gt;&lt;code&gt;@defer (on idle) {
  &lt;span class="nt"&gt;&amp;lt;app-defer&amp;gt;&amp;lt;/app-defer&amp;gt;&lt;/span&gt;
}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h3&gt;
  
  
  interaction
&lt;/h3&gt;

&lt;p&gt;The deferred loading will start when the given element is interacted with. The &lt;code&gt;interaction&lt;/code&gt; condition would accept a template reference variable as a parameter and will load its content when the element is interacted.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight xml"&gt;&lt;code&gt;&lt;span class="nt"&gt;&amp;lt;input&lt;/span&gt; &lt;span class="na"&gt;type=&lt;/span&gt;&lt;span class="s"&gt;"checkbox"&lt;/span&gt; &lt;span class="err"&gt;#check&lt;/span&gt;&lt;span class="nt"&gt;/&amp;gt;&lt;/span&gt;

@defer (on interaction(check)) {
 &lt;span class="nt"&gt;&amp;lt;app-defer&amp;gt;&amp;lt;/app-defer&amp;gt;&lt;/span&gt;
}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;blockquote&gt;
&lt;p&gt;So now you may have a question about the interaction. What are the events that are used for the &lt;code&gt;interaction&lt;/code&gt;? At the time of writing this post, the interaction is decided by two events — &lt;code&gt;click&lt;/code&gt; and &lt;code&gt;keydown&lt;/code&gt;. Probably there will be more events to be supported in the future.&lt;/p&gt;
&lt;/blockquote&gt;

&lt;h3&gt;
  
  
  hover
&lt;/h3&gt;

&lt;p&gt;The content will be loaded on the hover of the target element.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight xml"&gt;&lt;code&gt;&lt;span class="nt"&gt;&amp;lt;input&lt;/span&gt; &lt;span class="na"&gt;type=&lt;/span&gt;&lt;span class="s"&gt;"checkbox"&lt;/span&gt; &lt;span class="err"&gt;#check&lt;/span&gt;&lt;span class="nt"&gt;/&amp;gt;&lt;/span&gt;

@defer (on hover(check)) {
  &lt;span class="nt"&gt;&amp;lt;app-defer&amp;gt;&amp;lt;/app-defer&amp;gt;&lt;/span&gt;
}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h3&gt;
  
  
  viewport
&lt;/h3&gt;

&lt;p&gt;This is another fantastic declarative check to have. Now the defer block will load its content when the element specified in the viewport or the content itself is in the visible area.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight xml"&gt;&lt;code&gt;&lt;span class="nt"&gt;&amp;lt;div&lt;/span&gt; &lt;span class="err"&gt;#container&lt;/span&gt;&lt;span class="nt"&gt;&amp;gt;&lt;/span&gt;
  .....
&lt;span class="nt"&gt;&amp;lt;/div&amp;gt;&lt;/span&gt;

@defer (on viewport(container)) {
  &lt;span class="nt"&gt;&amp;lt;app-defer&amp;gt;&amp;lt;/app-defer&amp;gt;&lt;/span&gt;
}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;You can also mention the viewport condition to load its content when the actual content to be rendered is in the viewport area. But it will require you to mention the &lt;code&gt;@placeholder&lt;/code&gt; block by which the framework will find the viewport enter event.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight xml"&gt;&lt;code&gt;@defer (on viewport) {
  &lt;span class="nt"&gt;&amp;lt;app-defer&amp;gt;&amp;lt;/app-defer&amp;gt;&lt;/span&gt;
} @placeholder {
  &lt;span class="nt"&gt;&amp;lt;span&amp;gt;&lt;/span&gt;Loading&lt;span class="nt"&gt;&amp;lt;/span&amp;gt;&lt;/span&gt;
}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h3&gt;
  
  
  timer
&lt;/h3&gt;

&lt;p&gt;You can mention the timer trigger condition to load the content after a specific time. The below code will render the content after 2s.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight xml"&gt;&lt;code&gt;@defer (on timer(2000)) {
  &lt;span class="nt"&gt;&amp;lt;app-defer&amp;gt;&amp;lt;/app-defer&amp;gt;&lt;/span&gt;
} @placeholder {
  &lt;span class="nt"&gt;&amp;lt;span&amp;gt;&lt;/span&gt;Loading&lt;span class="nt"&gt;&amp;lt;/span&amp;gt;&lt;/span&gt;
}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;blockquote&gt;
&lt;p&gt;Disclaimer: The &lt;code&gt;timer&lt;/code&gt; logic was not yet added to the core package at the time of writing this post. Since the compiler support was added it will not show compile or runtime exceptions. Probably, this logic will be available in &lt;code&gt;Angular v17&lt;/code&gt;.&lt;/p&gt;
&lt;/blockquote&gt;

&lt;h2&gt;
  
  
  Using when (Imperative trigger conditions)
&lt;/h2&gt;

&lt;p&gt;Apart from using built-in trigger conditions, the developer can use &lt;code&gt;when&lt;/code&gt; to provide any custom logic to load the defer block. In the below code, the &lt;code&gt;canLoad&lt;/code&gt; method will return a boolean based on which the deferred content will be loaded.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight typescript"&gt;&lt;code&gt;&lt;span class="k"&gt;export&lt;/span&gt; &lt;span class="kd"&gt;class&lt;/span&gt; &lt;span class="nc"&gt;AppComponent&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="p"&gt;.&lt;/span&gt; &lt;span class="p"&gt;.&lt;/span&gt; &lt;span class="p"&gt;.&lt;/span&gt; &lt;span class="p"&gt;.&lt;/span&gt; &lt;span class="p"&gt;.&lt;/span&gt; &lt;span class="p"&gt;.&lt;/span&gt;
  &lt;span class="k"&gt;public&lt;/span&gt; &lt;span class="nf"&gt;canLoad&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="kc"&gt;true&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
  &lt;span class="p"&gt;}&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;





&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight xml"&gt;&lt;code&gt;@defer (when canLoad()) {
  &lt;span class="nt"&gt;&amp;lt;app-defer&amp;gt;&amp;lt;/app-defer&amp;gt;&lt;/span&gt;
}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;You can directly use &lt;code&gt;signals&lt;/code&gt;, public property, or methods in the when-trigger condition. You can also use logical operators to load based on multiple criteria.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight typescript"&gt;&lt;code&gt;&lt;span class="k"&gt;export&lt;/span&gt; &lt;span class="kd"&gt;class&lt;/span&gt; &lt;span class="nc"&gt;AppComponent&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="nx"&gt;showSignal&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nf"&gt;signal&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="kc"&gt;true&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
  &lt;span class="nx"&gt;show&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="kc"&gt;true&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;

  &lt;span class="k"&gt;public&lt;/span&gt; &lt;span class="nf"&gt;canLoad&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="kc"&gt;true&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
  &lt;span class="p"&gt;}&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;





&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight xml"&gt;&lt;code&gt;@defer (when (canLoad() &lt;span class="err"&gt;&amp;amp;&amp;amp;&lt;/span&gt; showSignal()) || show) {
  &lt;span class="nt"&gt;&amp;lt;app-defer&amp;gt;&amp;lt;/app-defer&amp;gt;&lt;/span&gt;
}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h2&gt;
  
  
  Can we use observables in the when trigger?
&lt;/h2&gt;

&lt;p&gt;I guess not, at the moment, passing observable to the &lt;code&gt;when&lt;/code&gt; trigger, it always resolved as &lt;code&gt;true&lt;/code&gt;. Using &lt;code&gt;async&lt;/code&gt; pipe or &lt;code&gt;toSignal&lt;/code&gt; also has no effect. Probably we can expect this in the main release or the Angular team might have a better explanation for this.&lt;/p&gt;

&lt;p&gt;If you use &lt;code&gt;async&lt;/code&gt; pipe, probably you will face the below error as I did.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight xml"&gt;&lt;code&gt;@defer (when show | async) { // show = of(true)
  &lt;span class="nt"&gt;&amp;lt;app-defer&amp;gt;&amp;lt;/app-defer&amp;gt;&lt;/span&gt;
} @placeholder {
  &lt;span class="nt"&gt;&amp;lt;span&amp;gt;&lt;/span&gt;Loading&lt;span class="nt"&gt;&amp;lt;/span&amp;gt;&lt;/span&gt;
}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fkij548iro3fymcj2eegm.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fkij548iro3fymcj2eegm.png" alt="Defer block - async pipe exception"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;h2&gt;
  
  
  Using prefetch
&lt;/h2&gt;

&lt;p&gt;The developer can prefetch the deferred block chunk files before rendering them. The &lt;code&gt;prefetch&lt;/code&gt; allows us to reduce the delay in loading the lazy chunk files which is required by the defer block.&lt;/p&gt;

&lt;p&gt;The &lt;code&gt;prefetch&lt;/code&gt; keyword is used to mention the prefetch condition in which the &lt;code&gt;on&lt;/code&gt; and &lt;code&gt;when&lt;/code&gt; can be used to mention the criteria required to prefetch the resource. A simple prefetch condition will look as below in which hovering the checkbox will prefetch the &lt;code&gt;app-defer&lt;/code&gt; chunk file and will be rendered when the button is interacted.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight xml"&gt;&lt;code&gt;&lt;span class="nt"&gt;&amp;lt;button&lt;/span&gt; &lt;span class="err"&gt;#button&lt;/span&gt;&lt;span class="nt"&gt;&amp;gt;&lt;/span&gt;Load Defer Component&lt;span class="nt"&gt;&amp;lt;/button&amp;gt;&lt;/span&gt;

&lt;span class="nt"&gt;&amp;lt;input&lt;/span&gt; &lt;span class="na"&gt;type=&lt;/span&gt;&lt;span class="s"&gt;"checkbox"&lt;/span&gt; &lt;span class="err"&gt;#check&lt;/span&gt;&lt;span class="nt"&gt;/&amp;gt;&lt;/span&gt;

@defer (on interaction(button); prefetch on hover(check)) {
 &lt;span class="nt"&gt;&amp;lt;app-defer&amp;gt;&amp;lt;/app-defer&amp;gt;&lt;/span&gt;
}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Ffy21w9612b5r1jhf9zgn.gif" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Ffy21w9612b5r1jhf9zgn.gif" alt="Prefetch defer block resource"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;h2&gt;
  
  
  Mixing trigger conditions
&lt;/h2&gt;

&lt;p&gt;You can use both &lt;code&gt;on&lt;/code&gt; and &lt;code&gt;when&lt;/code&gt; in a single defer block. All you need to ensure is that the &lt;code&gt;on&lt;/code&gt; and &lt;code&gt;when&lt;/code&gt; should be delimited by a semicolon &lt;code&gt;;&lt;/code&gt; as follows.&lt;/p&gt;

&lt;p&gt;The trigger conditions between &lt;code&gt;on&lt;/code&gt; and &lt;code&gt;when&lt;/code&gt; are combined using the logical &lt;code&gt;OR&lt;/code&gt; operator.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight xml"&gt;&lt;code&gt;@defer (on interaction(button),hover(check);
        when (canLoad() &lt;span class="err"&gt;&amp;amp;&amp;amp;&lt;/span&gt; showSignal())) {
 &lt;span class="nt"&gt;&amp;lt;app-defer&amp;gt;&amp;lt;/app-defer&amp;gt;&lt;/span&gt;
} @placeholder {
  &lt;span class="nt"&gt;&amp;lt;span&amp;gt;&lt;/span&gt;Loading&lt;span class="nt"&gt;&amp;lt;/span&amp;gt;&lt;/span&gt;
}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h2&gt;
  
  
  How to run the defer block in the Angular v17.0.0-next.6?
&lt;/h2&gt;

&lt;p&gt;Check out the below blog post on how to enable and run the defer block example using Angular v17.0.0-next.6.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://maddydeep28.medium.com/how-to-enable-the-new-control-flow-or-defer-block-in-the-angular-v17-0-0-next-6-6bbe29287cbe" rel="noopener noreferrer"&gt;https://maddydeep28.medium.com/how-to-enable-the-new-control-flow-or-defer-block-in-the-angular-v17-0-0-next-6-6bbe29287cbe&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Thanks for reading. If you like this article, please check out my &lt;a href="https://dev.to/madhust"&gt;articles here&lt;/a&gt; and follow me on &lt;a href="https://twitter.com/maddydeep28" rel="noopener noreferrer"&gt;Twitter(X)&lt;/a&gt; for more content like this.&lt;/p&gt;

</description>
      <category>angular</category>
      <category>javascript</category>
      <category>signals</category>
      <category>deferredloading</category>
    </item>
    <item>
      <title>How to enable the new control flow or defer block in the Angular v17.0.0-next.6?</title>
      <dc:creator>Madhu Sudhanan P</dc:creator>
      <pubDate>Sat, 30 Sep 2023 13:45:49 +0000</pubDate>
      <link>https://dev.to/madhust/how-to-enable-the-new-control-flow-or-defer-block-in-the-angular-v1700-next6-l6n</link>
      <guid>https://dev.to/madhust/how-to-enable-the-new-control-flow-or-defer-block-in-the-angular-v1700-next6-l6n</guid>
      <description>&lt;p&gt;Even though the new Angular control flows or the defer block changes are available on the release v17.0.0-next.6, you cannot directly use them now in your project and will end up with the below error.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;error NG5002: Invalid ICU message. Missing ‘}’ 
or 
error NG5002: Unexpected character “EOF”
(Do you have an unescaped “{“ in your template?
Use “{{ ‘{‘ }}”) to escape it.)
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;To try a new control flow or the defer block now you have to add the below config in the &lt;code&gt;angularCompileroptions&lt;/code&gt;.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight json"&gt;&lt;code&gt;&lt;span class="p"&gt;{&lt;/span&gt;&lt;span class="w"&gt;
  &lt;/span&gt;&lt;span class="nl"&gt;"angularCompilerOptions"&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="p"&gt;{&lt;/span&gt;&lt;span class="w"&gt;
    &lt;/span&gt;&lt;span class="err"&gt;....&lt;/span&gt;&lt;span class="w"&gt;
    &lt;/span&gt;&lt;span class="nl"&gt;"_enabledBlockTypes"&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="w"&gt;
      &lt;/span&gt;&lt;span class="s2"&gt;"if"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="s2"&gt;"switch"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="s2"&gt;"for"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="s2"&gt;"defer"&lt;/span&gt;&lt;span class="w"&gt;
    &lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt;&lt;span class="w"&gt;
  &lt;/span&gt;&lt;span class="p"&gt;}&lt;/span&gt;&lt;span class="w"&gt;
&lt;/span&gt;&lt;span class="p"&gt;}&lt;/span&gt;&lt;span class="w"&gt;
&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;code&gt;_enabledBlockTypes&lt;/code&gt; is an internal one that will be changed eventually. But for now, it will allow you to play around with the control flows.&lt;/p&gt;

&lt;p&gt;You can find the full working sample &lt;a href="https://github.com/Madhust/angular-control-flows"&gt;here&lt;/a&gt;.&lt;/p&gt;

&lt;p&gt;Feel free to let me know if you have any comments.&lt;/p&gt;

</description>
      <category>angular</category>
      <category>javascript</category>
      <category>signals</category>
      <category>webdev</category>
    </item>
    <item>
      <title>Angular — New Control flow with a working example</title>
      <dc:creator>Madhu Sudhanan P</dc:creator>
      <pubDate>Sat, 30 Sep 2023 13:34:36 +0000</pubDate>
      <link>https://dev.to/madhust/angular-new-control-flow-with-a-working-example-i48</link>
      <guid>https://dev.to/madhust/angular-new-control-flow-with-a-working-example-i48</guid>
      <description>&lt;p&gt;If you are watching recent Angular works, you’d probably know the addition of new control flows with new syntax. The new control flow is planned for the Angular v17 release that would probably face the public in November 2023. More details on why @ is chosen can be found &lt;a href="https://blog.angular.io/meet-angulars-new-control-flow-a02c6eee7843"&gt;here&lt;/a&gt;.&lt;/p&gt;

&lt;p&gt;In the Angular v17.0.0-next.6 release, the team has shipped the control flow changes which are not production-ready but we can play around with it.&lt;/p&gt;

&lt;p&gt;Let's see what are the available control flows.&lt;/p&gt;

&lt;h2&gt;
  
  
  If else statement
&lt;/h2&gt;

&lt;p&gt;In previous versions, &lt;code&gt;NgIf&lt;/code&gt; is used to show DOM elements conditionally which is declarative but is not suitable for the &lt;a href="https://maddydeep28.medium.com/angular-signals-new-change-detection-strategy-44f7ee9c359a"&gt;new change detection strategy&lt;/a&gt;. The new &lt;code&gt;@if&lt;/code&gt; block will resolve this problem and will work well with signal-based change detection.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight typescript"&gt;&lt;code&gt;&lt;span class="p"&gt;@&lt;/span&gt;&lt;span class="nd"&gt;Component&lt;/span&gt;&lt;span class="p"&gt;({&lt;/span&gt;
  &lt;span class="na"&gt;selector&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;app-root&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
  &lt;span class="na"&gt;templateUrl&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;./app.component.html&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
  &lt;span class="na"&gt;styleUrls&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;./app.component.scss&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt;
&lt;span class="p"&gt;})&lt;/span&gt;
&lt;span class="k"&gt;export&lt;/span&gt; &lt;span class="kd"&gt;class&lt;/span&gt; &lt;span class="nx"&gt;AppComponent&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="nx"&gt;show&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="kc"&gt;true&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
  &lt;span class="nx"&gt;showAnotherIf&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="kc"&gt;false&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;





&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight html"&gt;&lt;code&gt;@if (show) {
  &lt;span class="nt"&gt;&amp;lt;span&amp;gt;&lt;/span&gt;Inside if&lt;span class="nt"&gt;&amp;lt;/span&amp;gt;&lt;/span&gt;
} @else if (showAnotherIf) {
  &lt;span class="nt"&gt;&amp;lt;span&amp;gt;&lt;/span&gt;Inside else if&lt;span class="nt"&gt;&amp;lt;/span&amp;gt;&lt;/span&gt;
} @else {
  &lt;span class="nt"&gt;&amp;lt;span&amp;gt;&lt;/span&gt;Inside else&lt;span class="nt"&gt;&amp;lt;/span&amp;gt;&lt;/span&gt;
}

&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h2&gt;
  
  
  For loop
&lt;/h2&gt;

&lt;p&gt;The for loop allows you to render the given content based on the iterable object and it provides some useful context properties to work with. In addition, it provides a &lt;code&gt;@empty&lt;/code&gt; block which will be rendered when no item is present in the given array.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight typescript"&gt;&lt;code&gt;&lt;span class="p"&gt;@&lt;/span&gt;&lt;span class="nd"&gt;Component&lt;/span&gt;&lt;span class="p"&gt;({&lt;/span&gt;
  &lt;span class="na"&gt;selector&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;app-root&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
  &lt;span class="na"&gt;templateUrl&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;./app.component.html&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
  &lt;span class="na"&gt;styleUrls&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;./app.component.scss&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt;
&lt;span class="p"&gt;})&lt;/span&gt;
&lt;span class="k"&gt;export&lt;/span&gt; &lt;span class="kd"&gt;class&lt;/span&gt; &lt;span class="nx"&gt;AppComponent&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="nx"&gt;skills&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;javascript&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;html&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;css&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;];&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;

&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;





&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight html"&gt;&lt;code&gt;&lt;span class="nt"&gt;&amp;lt;ul&amp;gt;&lt;/span&gt;
  @for (item of skills; track item; let i = $index, f = $first; let l = $last, ev = $even, o = $odd; let co = $count) {
    &lt;span class="nt"&gt;&amp;lt;li&amp;gt;&lt;/span&gt;{{item}}
      &lt;span class="nt"&gt;&amp;lt;ul&amp;gt;&lt;/span&gt;
        &lt;span class="nt"&gt;&amp;lt;li&amp;gt;&lt;/span&gt;Index - {{i}}&lt;span class="nt"&gt;&amp;lt;/li&amp;gt;&lt;/span&gt;
        &lt;span class="nt"&gt;&amp;lt;li&amp;gt;&lt;/span&gt;Is First - {{f}}&lt;span class="nt"&gt;&amp;lt;/li&amp;gt;&lt;/span&gt;
        &lt;span class="nt"&gt;&amp;lt;li&amp;gt;&lt;/span&gt;Is Last - {{l}}&lt;span class="nt"&gt;&amp;lt;/li&amp;gt;&lt;/span&gt;
        &lt;span class="nt"&gt;&amp;lt;li&amp;gt;&lt;/span&gt;Is even - {{ev}}&lt;span class="nt"&gt;&amp;lt;/li&amp;gt;&lt;/span&gt;
        &lt;span class="nt"&gt;&amp;lt;li&amp;gt;&lt;/span&gt;Is odd - {{o}}&lt;span class="nt"&gt;&amp;lt;/li&amp;gt;&lt;/span&gt;
        &lt;span class="nt"&gt;&amp;lt;li&amp;gt;&lt;/span&gt;Count - {{co}}&lt;span class="nt"&gt;&amp;lt;/li&amp;gt;&lt;/span&gt;
      &lt;span class="nt"&gt;&amp;lt;/ul&amp;gt;&lt;/span&gt;
    &lt;span class="nt"&gt;&amp;lt;/li&amp;gt;&lt;/span&gt;
  } @empty {
    &lt;span class="nt"&gt;&amp;lt;li&amp;gt;&lt;/span&gt;No item&lt;span class="nt"&gt;&amp;lt;/li&amp;gt;&lt;/span&gt;
  }
&lt;span class="nt"&gt;&amp;lt;/ul&amp;gt;&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h2&gt;
  
  
  Switch case
&lt;/h2&gt;

&lt;p&gt;The new switch case control flow can be used as follows. You can use &lt;code&gt;@default&lt;/code&gt; block to mention the default content to be rendered when no case is matched.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight typescript"&gt;&lt;code&gt;&lt;span class="p"&gt;@&lt;/span&gt;&lt;span class="nd"&gt;Component&lt;/span&gt;&lt;span class="p"&gt;({&lt;/span&gt;
  &lt;span class="na"&gt;selector&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;app-root&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
  &lt;span class="na"&gt;templateUrl&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;./app.component.html&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
  &lt;span class="na"&gt;styleUrls&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;./app.component.scss&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt;
&lt;span class="p"&gt;})&lt;/span&gt;
&lt;span class="k"&gt;export&lt;/span&gt; &lt;span class="kd"&gt;class&lt;/span&gt; &lt;span class="nx"&gt;AppComponent&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="nx"&gt;caseNo&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="mi"&gt;4&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;





&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight html"&gt;&lt;code&gt;@switch (caseNo) {
  @case (1) {
    &lt;span class="nt"&gt;&amp;lt;span&amp;gt;&lt;/span&gt;Rendering case 1&lt;span class="nt"&gt;&amp;lt;/span&amp;gt;&lt;/span&gt;
  }
  @case (2) {
    &lt;span class="nt"&gt;&amp;lt;span&amp;gt;&lt;/span&gt;Rendering case 2&lt;span class="nt"&gt;&amp;lt;/span&amp;gt;&lt;/span&gt;
  }
  @case (3) {
    &lt;span class="nt"&gt;&amp;lt;span&amp;gt;&lt;/span&gt;Rendering case 3&lt;span class="nt"&gt;&amp;lt;/span&amp;gt;&lt;/span&gt;
  }
  @default {
    &lt;span class="nt"&gt;&amp;lt;span&amp;gt;&lt;/span&gt;Rendering default&lt;span class="nt"&gt;&amp;lt;/span&amp;gt;&lt;/span&gt;
  }
}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h2&gt;
  
  
  How to enable the new control flow in the Angular v17.0.0-next.6?
&lt;/h2&gt;

&lt;p&gt;Even though the changes are available on the package, you cannot directly use this now and will end up with the below error.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;error NG5002: Invalid ICU message. Missing ‘}’ 
or 
error NG5002: Unexpected character “EOF”
(Do you have an unescaped “{“ in your template? 
Use “{{ ‘{‘ }}”) to escape it.)
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;To try the new control flows now you have to add the below config in the &lt;code&gt;angularCompileroptions&lt;/code&gt;.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight json"&gt;&lt;code&gt;&lt;span class="p"&gt;{&lt;/span&gt;&lt;span class="w"&gt;
  &lt;/span&gt;&lt;span class="nl"&gt;"angularCompilerOptions"&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="p"&gt;{&lt;/span&gt;&lt;span class="w"&gt;
    &lt;/span&gt;&lt;span class="err"&gt;....&lt;/span&gt;&lt;span class="w"&gt;
    &lt;/span&gt;&lt;span class="nl"&gt;"_enabledBlockTypes"&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="w"&gt;
      &lt;/span&gt;&lt;span class="s2"&gt;"if"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="s2"&gt;"switch"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="s2"&gt;"for"&lt;/span&gt;&lt;span class="w"&gt;
    &lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt;&lt;span class="w"&gt;
  &lt;/span&gt;&lt;span class="p"&gt;}&lt;/span&gt;&lt;span class="w"&gt;
&lt;/span&gt;&lt;span class="p"&gt;}&lt;/span&gt;&lt;span class="w"&gt;
&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;I got your mind's voice, yeah &lt;code&gt;_enabledBlockTypes&lt;/code&gt; is an internal one that will be changed eventually. But for now, it will allow you to play around with the control flows.&lt;/p&gt;

&lt;p&gt;You can find the full working sample &lt;a href="https://github.com/Madhust/angular-control-flows/"&gt;here&lt;/a&gt;.&lt;/p&gt;

&lt;p&gt;Feel free to let me know if you have any comments.&lt;/p&gt;

</description>
      <category>angular</category>
      <category>javascript</category>
      <category>signals</category>
      <category>webdev</category>
    </item>
    <item>
      <title>Angular Signals — Using the untracked() Function to Prevent Dependency Tracking</title>
      <dc:creator>Madhu Sudhanan P</dc:creator>
      <pubDate>Tue, 12 Sep 2023 06:28:38 +0000</pubDate>
      <link>https://dev.to/madhust/angular-signals-using-the-untracked-function-to-prevent-dependency-tracking-2knd</link>
      <guid>https://dev.to/madhust/angular-signals-using-the-untracked-function-to-prevent-dependency-tracking-2knd</guid>
      <description>&lt;p&gt;Before going through this article, if you are not familiar with Angular signals, I’d recommend reading my previous post on signals — &lt;a href="https://dev.to/madhust/angular-signals-new-change-detection-strategy-23b0"&gt;Angular Signals🚦: New Change detection strategy.&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;For every change in the signal, computed and effect functions that are interested in the signal, will be recomputed &amp;amp; executed respectively. There are cases when we don't want this recomputation to occur for the signal change. In Angular signals, all the dependencies will run inside a reactive context, to avoid this recomputation, it should be run inside a non-reactive context.&lt;/p&gt;

&lt;p&gt;Angular provided untracked function which allows you to execute any function in a non-reactive context and sometimes it will return values.&lt;/p&gt;

&lt;p&gt;Let’s see some of the use cases of the untracked function.&lt;/p&gt;

&lt;h3&gt;
  
  
  Calculate the computed signal property only once
&lt;/h3&gt;

&lt;p&gt;In some cases, you might want to have a computed signal that will compute its value only once and it will not care about further change. In such cases, you can use untracked function inside the computed signal to read signal value without tracking dependencies.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight typescript"&gt;&lt;code&gt;&lt;span class="k"&gt;export&lt;/span&gt; &lt;span class="kd"&gt;class&lt;/span&gt; &lt;span class="nx"&gt;AppComponent&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;

  &lt;span class="nx"&gt;count&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;signal&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="mi"&gt;1&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;

  &lt;span class="nx"&gt;revenue&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;computed&lt;/span&gt;&lt;span class="p"&gt;(()&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="nx"&gt;untracked&lt;/span&gt;&lt;span class="p"&gt;(()&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="k"&gt;this&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;count&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt; &lt;span class="o"&gt;*&lt;/span&gt; &lt;span class="mi"&gt;5&lt;/span&gt;&lt;span class="p"&gt;));&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;In this above code, the revenue computed signal will be computed only once(value would be 5) based on its dependency count. For subsequent set operations, revenue will not recalculate and will remain in the same value.&lt;/p&gt;

&lt;h3&gt;
  
  
  Reading without tracking dependencies in effect method
&lt;/h3&gt;

&lt;p&gt;This is a very common use that was pointed out by the angular team. You can use this method to have an untracked reading of the signal values inside the effect method so that for further change in the signal, the effect will not be invoked.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight typescript"&gt;&lt;code&gt;&lt;span class="k"&gt;export&lt;/span&gt; &lt;span class="kd"&gt;class&lt;/span&gt; &lt;span class="nx"&gt;AppComponent&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;

  &lt;span class="nx"&gt;count&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;signal&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="mi"&gt;1&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;

  &lt;span class="nx"&gt;revenue&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;computed&lt;/span&gt;&lt;span class="p"&gt;(()&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="nx"&gt;untracked&lt;/span&gt;&lt;span class="p"&gt;(()&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="k"&gt;this&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;count&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt; &lt;span class="o"&gt;*&lt;/span&gt; &lt;span class="mi"&gt;5&lt;/span&gt;&lt;span class="p"&gt;));&lt;/span&gt;

  &lt;span class="kd"&gt;constructor&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="nx"&gt;effect&lt;/span&gt;&lt;span class="p"&gt;(()&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
      &lt;span class="nx"&gt;console&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;log&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;Count effect invoked &lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt; &lt;span class="o"&gt;+&lt;/span&gt; &lt;span class="nx"&gt;untracked&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="k"&gt;this&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;count&lt;/span&gt;&lt;span class="p"&gt;));&lt;/span&gt;
    &lt;span class="p"&gt;});&lt;/span&gt;
  &lt;span class="p"&gt;}&lt;/span&gt;

&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h3&gt;
  
  
  Unwrap signal value
&lt;/h3&gt;

&lt;p&gt;Well, this is a very simple use case, you will probably not need it but I think it's worth mentioning. The untracked function will return a value that we can use. You can use this function to simply unwrap or execute any arbitrary function.&lt;/p&gt;

&lt;p&gt;In the below code, nCount simply unwrap the signal value and factor will hold the result of the arbitrary function run inside the untracked function.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight typescript"&gt;&lt;code&gt;&lt;span class="k"&gt;export&lt;/span&gt; &lt;span class="kd"&gt;class&lt;/span&gt; &lt;span class="nx"&gt;AppComponent&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;

  &lt;span class="nx"&gt;count&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;signal&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="mi"&gt;1&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
  &lt;span class="nx"&gt;users&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;signal&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="mi"&gt;0&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
  &lt;span class="nx"&gt;nCount&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;untracked&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="k"&gt;this&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;count&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
  &lt;span class="nx"&gt;factor&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;untracked&lt;/span&gt;&lt;span class="p"&gt;(()&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt; 
              &lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;count&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="k"&gt;this&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;count&lt;/span&gt;&lt;span class="p"&gt;();&lt;/span&gt;
              &lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;users&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="k"&gt;this&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;users&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt; &lt;span class="o"&gt;||&lt;/span&gt; &lt;span class="mi"&gt;5&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
              &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="nx"&gt;count&lt;/span&gt; &lt;span class="o"&gt;/&lt;/span&gt; &lt;span class="nx"&gt;users&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
            &lt;span class="p"&gt;});&lt;/span&gt;

  &lt;span class="nx"&gt;revenue&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;computed&lt;/span&gt;&lt;span class="p"&gt;(()&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="nx"&gt;untracked&lt;/span&gt;&lt;span class="p"&gt;(()&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="k"&gt;this&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;count&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt; &lt;span class="o"&gt;*&lt;/span&gt; &lt;span class="mi"&gt;5&lt;/span&gt;&lt;span class="p"&gt;));&lt;/span&gt;

  &lt;span class="kd"&gt;constructor&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="nx"&gt;effect&lt;/span&gt;&lt;span class="p"&gt;(()&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
      &lt;span class="nx"&gt;console&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;log&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;Count effect invoked &lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt; &lt;span class="o"&gt;+&lt;/span&gt; &lt;span class="nx"&gt;untracked&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="k"&gt;this&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;count&lt;/span&gt;&lt;span class="p"&gt;));&lt;/span&gt;
    &lt;span class="p"&gt;});&lt;/span&gt;
  &lt;span class="p"&gt;}&lt;/span&gt;

&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h3&gt;
  
  
  Is it possible to set/update/mutate any signal value without notifying the dependencies using an untracked function?
&lt;/h3&gt;

&lt;p&gt;The answer is a big NO.&lt;/p&gt;

&lt;p&gt;Setting or changing the signal values inside an untracked function will not prevent it from tracking and notifying the dependencies.&lt;/p&gt;

&lt;p&gt;Setting the signal value inside the untracked function will track dependencies and notify the interested consumers.&lt;/p&gt;

&lt;p&gt;In the below code, setting users signal inside the untracked function will not prevent it from notifying the consumers and the effect function that uses the users function will be called.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight typescript"&gt;&lt;code&gt;&lt;span class="k"&gt;export&lt;/span&gt; &lt;span class="kd"&gt;class&lt;/span&gt; &lt;span class="nx"&gt;AppComponent&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;

  &lt;span class="nx"&gt;toggle&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;signal&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="kc"&gt;true&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
  &lt;span class="nx"&gt;count&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;signal&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="mi"&gt;1&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
  &lt;span class="nx"&gt;users&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;signal&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="mi"&gt;0&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
  &lt;span class="nx"&gt;nCount&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;untracked&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="k"&gt;this&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;count&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
  &lt;span class="nx"&gt;factor&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;untracked&lt;/span&gt;&lt;span class="p"&gt;(()&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt; 
              &lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;count&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="k"&gt;this&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;count&lt;/span&gt;&lt;span class="p"&gt;();&lt;/span&gt;
              &lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;users&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="k"&gt;this&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;users&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt; &lt;span class="o"&gt;||&lt;/span&gt; &lt;span class="mi"&gt;5&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;

              &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="nx"&gt;count&lt;/span&gt; &lt;span class="o"&gt;/&lt;/span&gt; &lt;span class="nx"&gt;users&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
            &lt;span class="p"&gt;});&lt;/span&gt;

  &lt;span class="nx"&gt;revenue&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;computed&lt;/span&gt;&lt;span class="p"&gt;(()&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="nx"&gt;untracked&lt;/span&gt;&lt;span class="p"&gt;(()&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="k"&gt;this&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;count&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt; &lt;span class="o"&gt;*&lt;/span&gt; &lt;span class="mi"&gt;5&lt;/span&gt;&lt;span class="p"&gt;));&lt;/span&gt;

  &lt;span class="kd"&gt;constructor&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="nx"&gt;effect&lt;/span&gt;&lt;span class="p"&gt;(()&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
      &lt;span class="nx"&gt;console&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;log&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;Empty effect method&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
      &lt;span class="c1"&gt;// SETTING THE users signal &lt;/span&gt;
      &lt;span class="nx"&gt;untracked&lt;/span&gt;&lt;span class="p"&gt;(()&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="k"&gt;this&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;users&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="kd"&gt;set&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="mi"&gt;1&lt;/span&gt;&lt;span class="p"&gt;));&lt;/span&gt;
    &lt;span class="p"&gt;});&lt;/span&gt;

    &lt;span class="nx"&gt;effect&lt;/span&gt;&lt;span class="p"&gt;(()&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
      &lt;span class="c1"&gt;// Will be called when &lt;/span&gt;
      &lt;span class="c1"&gt;// users signal is set in the previous effect method.&lt;/span&gt;
      &lt;span class="nx"&gt;console&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;log&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;User effect invoked &lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt; &lt;span class="o"&gt;+&lt;/span&gt; &lt;span class="k"&gt;this&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;users&lt;/span&gt;&lt;span class="p"&gt;());&lt;/span&gt;
    &lt;span class="p"&gt;});&lt;/span&gt;
  &lt;span class="p"&gt;}&lt;/span&gt;

&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;I hope you like reading this article. If you find it useful, please follow me on Medium or &lt;a href="https://twitter.com/maddydeep28"&gt;Twitter &lt;/a&gt;for content like this.&lt;/p&gt;

&lt;p&gt;Happy coding…&lt;/p&gt;

</description>
      <category>angular</category>
      <category>javascript</category>
      <category>react</category>
      <category>signal</category>
    </item>
    <item>
      <title>Angular Signals🚦: New Change detection strategy</title>
      <dc:creator>Madhu Sudhanan P</dc:creator>
      <pubDate>Tue, 12 Sep 2023 06:18:12 +0000</pubDate>
      <link>https://dev.to/madhust/angular-signals-new-change-detection-strategy-23b0</link>
      <guid>https://dev.to/madhust/angular-signals-new-change-detection-strategy-23b0</guid>
      <description>&lt;p&gt;The hype around Angular signals is real. If you are an Angular user, I hope, you already know the answer. Signal is going to be the new change detection strategy.&lt;/p&gt;

&lt;p&gt;There are many articles available online now that are deep-diving into the Signals concepts. I suppose it’s also worth sharing my understanding of the concept.&lt;/p&gt;

&lt;h2&gt;
  
  
  Change detection: What is the current state and What will be the future?
&lt;/h2&gt;

&lt;p&gt;Angular is currently relying on the Zone js for the change detection. Zone Js will monkey patch various built-in APIs to notify the change in the application. If a new Web API is introduced in JavaScript then Zone Js has to be updated by monkey patching the new API so relying on Zone Js is not a future-proof solution.&lt;/p&gt;

&lt;p&gt;Another major setback with this approach is that there is no information about the component that is responsible for the change and what changed in the model. Due to this, to reflect the model changes in the DOM, the application has to traverse from top to down to identify and apply the changes. The component traversal can be customized by the OnPush change detection strategy though.&lt;/p&gt;

&lt;p&gt;Also, many users like to have zoneless applications and various experiments can be seen online.&lt;/p&gt;

&lt;p&gt;To tackle these problems, Angular started to revisit its current change detection strategy which results in the signals concepts that provide fine-grained control over the component change detection.&lt;/p&gt;

&lt;p&gt;Signals bring the below advantages to Angular.&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Updates only the components that interest the model value change by providing the information on the model change.&lt;/li&gt;
&lt;li&gt;Removes top-down component traversal. It only updates the component that uses the signal.&lt;/li&gt;
&lt;li&gt;No dirty monkey patching of global methods/APIs.&lt;/li&gt;
&lt;/ul&gt;

&lt;h2&gt;
  
  
  Building Blocks of Angular Signal
&lt;/h2&gt;

&lt;p&gt;The below items are the major building blocks of the Angular signal.&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Signal&lt;/li&gt;
&lt;li&gt;Computed signal&lt;/li&gt;
&lt;li&gt;Effect&lt;/li&gt;
&lt;/ul&gt;

&lt;h3&gt;
  
  
  What is signal?
&lt;/h3&gt;

&lt;p&gt;Signal is a lightweight wrapper around a value that notifies the value changes to the interested consumers. It comes with several methods that allow us to set, update, and mutate its value.&lt;/p&gt;

&lt;p&gt;You can initialize the signal value by using the signal method.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight typescript"&gt;&lt;code&gt;&lt;span class="k"&gt;export&lt;/span&gt; &lt;span class="kd"&gt;class&lt;/span&gt; &lt;span class="nx"&gt;AppComponent&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="nx"&gt;count&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;signal&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="mi"&gt;0&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;code&gt;set&lt;/code&gt; method can be used to change the initialization value of the signal. The signal value is changed to a new value in the resetView method.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight typescript"&gt;&lt;code&gt;&lt;span class="k"&gt;export&lt;/span&gt; &lt;span class="kd"&gt;class&lt;/span&gt; &lt;span class="nx"&gt;AppComponent&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;

  &lt;span class="nx"&gt;count&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;signal&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="mi"&gt;0&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;

  &lt;span class="nx"&gt;resetView&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="k"&gt;this&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;count&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="kd"&gt;set&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="mi"&gt;0&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
  &lt;span class="p"&gt;}&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;update method will help you to change the value based on the previous value.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight typescript"&gt;&lt;code&gt;&lt;span class="k"&gt;export&lt;/span&gt; &lt;span class="kd"&gt;class&lt;/span&gt; &lt;span class="nx"&gt;AppComponent&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="nx"&gt;count&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;signal&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="mi"&gt;0&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;

  &lt;span class="nx"&gt;addView&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="k"&gt;this&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;count&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;update&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;val&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="nx"&gt;val&lt;/span&gt; &lt;span class="o"&gt;+&lt;/span&gt; &lt;span class="mi"&gt;1&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
  &lt;span class="p"&gt;}&lt;/span&gt;

  &lt;span class="nx"&gt;resetView&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="k"&gt;this&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;count&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="kd"&gt;set&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="mi"&gt;0&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
  &lt;span class="p"&gt;}&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;&lt;span class="s2"&gt;`
&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;code&gt;mutate&lt;/code&gt; method will allow you to mutate the signal in its place and notify its dependencies.&lt;/p&gt;

&lt;h3&gt;
  
  
  Computed signal properties
&lt;/h3&gt;

&lt;p&gt;Another interesting part of the signal is the computed signal properties. The computed signal is a long-requested feature in Angular and, introducing it as a part of the signal has major advantages for both developer experience and performance of the application.&lt;/p&gt;

&lt;p&gt;The computed function can be used to create a computed signal property.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight typescript"&gt;&lt;code&gt;&lt;span class="k"&gt;export&lt;/span&gt; &lt;span class="kd"&gt;class&lt;/span&gt; &lt;span class="nx"&gt;AppComponent&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;

  &lt;span class="nx"&gt;count&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;signal&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="mi"&gt;0&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;

  &lt;span class="nx"&gt;revenue&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;computed&lt;/span&gt;&lt;span class="p"&gt;(()&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="k"&gt;this&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;count&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt; &lt;span class="o"&gt;*&lt;/span&gt; &lt;span class="mf"&gt;0.5&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;

  &lt;span class="nx"&gt;addView&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="k"&gt;this&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;count&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;update&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;val&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="nx"&gt;val&lt;/span&gt; &lt;span class="o"&gt;+&lt;/span&gt; &lt;span class="mi"&gt;1&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
  &lt;span class="p"&gt;}&lt;/span&gt;

  &lt;span class="nx"&gt;resetView&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="k"&gt;this&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;count&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="kd"&gt;set&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="mi"&gt;0&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
  &lt;span class="p"&gt;}&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;The computed signal property has the following behavior.&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Computed properties are lazily initialized. The computation will not happen until the value is accessed.&lt;/li&gt;
&lt;li&gt;It shuffles the dependency graph dynamically based on the dependency signal change. Angular is being cautious in the dynamic dependency graph since the frequent setting of the dependency graph will come at a cost.&lt;/li&gt;
&lt;/ul&gt;

&lt;h3&gt;
  
  
  Effect
&lt;/h3&gt;

&lt;p&gt;The effect function allows you to listen to the signal changes and act based on them. The effects can be used to handle some use cases like,&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Custom updating DOM after one or more signals are changed.&lt;/li&gt;
&lt;li&gt;Trigger network requests.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;The effect will auto-subscribe itself based on the signals used in its context. For example, the below list of effect methods used in the constructor will be called in various signal changes.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight typescript"&gt;&lt;code&gt;&lt;span class="k"&gt;export&lt;/span&gt; &lt;span class="kd"&gt;class&lt;/span&gt; &lt;span class="nx"&gt;AppComponent&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="nx"&gt;count&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;signal&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="mi"&gt;0&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
  &lt;span class="nx"&gt;user&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;signal&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="mi"&gt;0&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;

  &lt;span class="kd"&gt;constructor&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
      &lt;span class="nx"&gt;effect&lt;/span&gt;&lt;span class="p"&gt;(()&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
        &lt;span class="nx"&gt;console&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;log&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;Empty effect method&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
      &lt;span class="p"&gt;});&lt;/span&gt;

      &lt;span class="nx"&gt;effect&lt;/span&gt;&lt;span class="p"&gt;(()&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
        &lt;span class="nx"&gt;console&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;log&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;User effect invoked &lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt; &lt;span class="o"&gt;+&lt;/span&gt; &lt;span class="k"&gt;this&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;user&lt;/span&gt;&lt;span class="p"&gt;());&lt;/span&gt;
      &lt;span class="p"&gt;});&lt;/span&gt;

      &lt;span class="nx"&gt;effect&lt;/span&gt;&lt;span class="p"&gt;(()&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
        &lt;span class="nx"&gt;console&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;log&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;User and View effect invoked &lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt; &lt;span class="o"&gt;+&lt;/span&gt; &lt;span class="k"&gt;this&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;user&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt; &lt;span class="o"&gt;+&lt;/span&gt; &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt; &lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt; &lt;span class="o"&gt;+&lt;/span&gt; &lt;span class="k"&gt;this&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;count&lt;/span&gt;&lt;span class="p"&gt;());&lt;/span&gt;
      &lt;span class="p"&gt;});&lt;/span&gt;

      &lt;span class="nx"&gt;effect&lt;/span&gt;&lt;span class="p"&gt;(()&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
        &lt;span class="nx"&gt;console&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;log&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;Count effect invoked &lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt; &lt;span class="o"&gt;+&lt;/span&gt; &lt;span class="nx"&gt;untracked&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="k"&gt;this&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;count&lt;/span&gt;&lt;span class="p"&gt;));&lt;/span&gt;
      &lt;span class="p"&gt;});&lt;/span&gt;
  &lt;span class="p"&gt;}&lt;/span&gt;

  &lt;span class="nx"&gt;addView&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="k"&gt;this&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;count&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;update&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;val&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="nx"&gt;val&lt;/span&gt; &lt;span class="o"&gt;+&lt;/span&gt; &lt;span class="mi"&gt;1&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
  &lt;span class="p"&gt;}&lt;/span&gt;

  &lt;span class="nx"&gt;userChange&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="k"&gt;this&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;user&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;update&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;val&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="nx"&gt;val&lt;/span&gt; &lt;span class="o"&gt;+&lt;/span&gt; &lt;span class="mi"&gt;1&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
  &lt;span class="p"&gt;}&lt;/span&gt;

  &lt;span class="nx"&gt;resetView&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="k"&gt;this&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;count&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="kd"&gt;set&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="mi"&gt;0&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
  &lt;span class="p"&gt;}&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;This effect method will be called only once since no signal property is used in its context.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight typescript"&gt;&lt;code&gt;&lt;span class="nx"&gt;effect&lt;/span&gt;&lt;span class="p"&gt;(()&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="nx"&gt;console&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;log&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;Empty effect method&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
&lt;span class="p"&gt;});&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;The below effect will be called whenever the user signal is changed.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight typescript"&gt;&lt;code&gt;&lt;span class="nx"&gt;effect&lt;/span&gt;&lt;span class="p"&gt;(()&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="nx"&gt;console&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;log&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;User effect invoked &lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt; &lt;span class="o"&gt;+&lt;/span&gt; &lt;span class="k"&gt;this&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;user&lt;/span&gt;&lt;span class="p"&gt;());&lt;/span&gt;
&lt;span class="p"&gt;});&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;This callback will be invoked when either the user or count signal is changed.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight typescript"&gt;&lt;code&gt;&lt;span class="nx"&gt;effect&lt;/span&gt;&lt;span class="p"&gt;(()&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="nx"&gt;console&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;log&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;User and View effect invoked &lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt; 
               &lt;span class="o"&gt;+&lt;/span&gt; &lt;span class="k"&gt;this&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;user&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt; &lt;span class="o"&gt;+&lt;/span&gt; &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt; &lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt; &lt;span class="o"&gt;+&lt;/span&gt; &lt;span class="k"&gt;this&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;count&lt;/span&gt;&lt;span class="p"&gt;());&lt;/span&gt;
&lt;span class="p"&gt;});&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;This effect method will be called when the very first time the count signal is set then it will become inert since the count is wrapped in the untracked method. This method will help you execute the signals inside a non-reactive context.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight typescript"&gt;&lt;code&gt;&lt;span class="nx"&gt;effect&lt;/span&gt;&lt;span class="p"&gt;(()&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="nx"&gt;console&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;log&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;Count effect invoked &lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt; &lt;span class="o"&gt;+&lt;/span&gt; &lt;span class="nx"&gt;untracked&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="k"&gt;this&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;count&lt;/span&gt;&lt;span class="p"&gt;));&lt;/span&gt;
&lt;span class="p"&gt;});&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h2&gt;
  
  
  Using signals in the templates
&lt;/h2&gt;

&lt;p&gt;The angular team chooses the getter function approach to use signals inside the templates.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight html"&gt;&lt;code&gt;&lt;span class="nt"&gt;&amp;lt;div&amp;gt;&lt;/span&gt;
  &lt;span class="nt"&gt;&amp;lt;span&amp;gt;&lt;/span&gt;Unique users: &lt;span class="nt"&gt;&amp;lt;/span&amp;gt;&lt;/span&gt;
  &lt;span class="nt"&gt;&amp;lt;span&amp;gt;&lt;/span&gt;{{users()}}&lt;span class="nt"&gt;&amp;lt;/span&amp;gt;&lt;/span&gt;
&lt;span class="nt"&gt;&amp;lt;/div&amp;gt;&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;The team sees some clear advantages in using the getter function approach like it implies that it is meant for read operation and maintains consistency between the component and template files.&lt;/p&gt;

&lt;p&gt;Over the years Angular developers learned not to use functions inside the template since it would have a considerable performance impact if the method did some heavy work. But it's not the case with signals since they are built to do computations in a minimal way.&lt;/p&gt;

&lt;h3&gt;
  
  
  Upcoming works — signals
&lt;/h3&gt;

&lt;p&gt;Well, signals in Angular is a bit large topic and I just covered a very small part of it. The angular team is still working on introducing several changes in the input/output, control flow and defer loading to improve the developer experience and ability to add Zone-less(Fully signal)-based applications in the future.&lt;/p&gt;

&lt;p&gt;If you find this article helpful, please follow me here and on &lt;a href="https://twitter.com/maddydeep28"&gt;twitter &lt;/a&gt;for more content like this.&lt;/p&gt;

&lt;p&gt;Happy coding…&lt;/p&gt;

</description>
      <category>angular</category>
      <category>javascript</category>
      <category>react</category>
      <category>signals</category>
    </item>
    <item>
      <title>Stateless standalone components in Angular</title>
      <dc:creator>Madhu Sudhanan P</dc:creator>
      <pubDate>Tue, 07 Jun 2022 15:43:16 +0000</pubDate>
      <link>https://dev.to/madhust/stateless-standalone-components-in-angular-9gl</link>
      <guid>https://dev.to/madhust/stateless-standalone-components-in-angular-9gl</guid>
      <description>&lt;p&gt;Such an exciting week for the angular community since the Angular v14 is released. Angular v14 is packed with amazing works like Typed reactive form, Page title resolver, standalone component, inject function, etc.&lt;/p&gt;

&lt;p&gt;Standalone component &amp;amp; inject function interests me more than anything as it's paving the way to reactify the angular. The term "reactify" may be funny but these features will give the developers who shifted to React will allow them to look back to the Angular once again and this term made me think about another popular terminology of React, Stateless component.&lt;/p&gt;

&lt;p&gt;Stateless component is enforced in React and it's being part of the framework. When comes to angular, stateless components are not popular as in react. Since the standalone component has been introduced to the Angular, I suppose, the Stateless Standalone component will also be a talk in near future.&lt;/p&gt;

&lt;p&gt;So how to make your angular component stateless?&lt;/p&gt;

&lt;p&gt;I think the answer is very simple.&lt;br&gt;
 &lt;br&gt;
&lt;strong&gt;&lt;em&gt;"DON'T MUTATE THE INCOMING PROPERTIES".&lt;/em&gt;&lt;/strong&gt; &lt;/p&gt;

&lt;p&gt;All the mutations should be handled at the container/parent component. That's it. Now your angular component will become stateless or presentational or any other name you call it.&lt;br&gt;
Let's see how a stateless component can be implemented in angular.&lt;/p&gt;


&lt;div class="ltag_gist-liquid-tag"&gt;
  
&lt;/div&gt;


&lt;p&gt;The above component is a simple stateless standalone component in which title property is not mutated inside the component rather the changes have been emitted to the parent component. The parent component which will act as the container component will handle the mutation.&lt;/p&gt;

&lt;p&gt;The stateless component provides several advantages such as below.&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;Less code and easy to maintain.&lt;/li&gt;
&lt;li&gt;Performance - When combined with the OnPush strategy, the stateless component might provide better performance.&lt;/li&gt;
&lt;li&gt;Easy to test since the result will be the same for input and no side effects.&lt;/li&gt;
&lt;li&gt;Ensure a single source of truth when working with state management libraries.&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;Now we have seen how to create a simple stateless component but there is a chance that another developer makes the component stateful which is meant to be stateless.&lt;/p&gt;

&lt;h2&gt;
  
  
  StatelessComponent decorator
&lt;/h2&gt;

&lt;p&gt;&lt;strong&gt;&lt;em&gt;StatelessComponent&lt;/em&gt;&lt;/strong&gt; decorator of the &lt;a href="https://www.npmjs.com/package/@protools/angular"&gt;@protools/angular&lt;/a&gt; package will help you to ensure and maintain the statelessness of your angular component by respecting this simple rule &lt;strong&gt;&lt;em&gt;"DON'T MUTATE THE INCOMING PROPERTIES".&lt;/em&gt;&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;To make your angular component stateless simply add the &lt;strong&gt;&lt;em&gt;StatelessComponent&lt;/em&gt;&lt;/strong&gt; decorator from the &lt;a href="https://www.npmjs.com/package/@protools/angular"&gt;@protools/angular&lt;/a&gt; package.&lt;/p&gt;


&lt;div class="ltag_gist-liquid-tag"&gt;
  
&lt;/div&gt;


&lt;h3&gt;
  
  
  Exclude methods
&lt;/h3&gt;

&lt;p&gt;You can exclude methods from preserving the statelessness of the component which means you can change the input properties within those methods. To exclude any method, provide the &lt;code&gt;excludeMethods&lt;/code&gt; option of the StatelessComponent decorator. This helps you to provide two-way binding support for a property.&lt;/p&gt;


&lt;div class="ltag_gist-liquid-tag"&gt;
  
&lt;/div&gt;


&lt;p&gt;That's it. Now your component became stateless and it will ensure the statelessness during further changes too.&lt;/p&gt;

&lt;p&gt;Take a look at the @protools/angular package below and give it a try.&lt;br&gt;
&lt;a href="https://www.npmjs.com/package/@protools/angular"&gt;https://www.npmjs.com/package/@protools/angular&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Feel free to contact me. Happy coding!!&lt;/p&gt;

</description>
      <category>angular</category>
      <category>statelesscomponent</category>
      <category>javascript</category>
      <category>decorators</category>
    </item>
    <item>
      <title>Lazy loading non-routable Angular modules — Imperative &amp; Declarative pattern</title>
      <dc:creator>Madhu Sudhanan P</dc:creator>
      <pubDate>Sun, 01 May 2022 03:59:26 +0000</pubDate>
      <link>https://dev.to/madhust/lazy-loading-non-routable-angular-modules-imperative-declarative-pattern-3a33</link>
      <guid>https://dev.to/madhust/lazy-loading-non-routable-angular-modules-imperative-declarative-pattern-3a33</guid>
      <description>&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--m9m_TlDK--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/mrwbo2xug0vohsgqq9ek.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--m9m_TlDK--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/mrwbo2xug0vohsgqq9ek.png" alt="Image description" width="880" height="695"&gt;&lt;/a&gt;&lt;br&gt;
Lazy loading modules in angular enable us to split code into separate modules and load them on demand, this helps developers to have maintainable code and better performance.&lt;/p&gt;

&lt;p&gt;By default, angular lazily load modules based on the route path. Lazy loading modules based on the route path suits most cases but sometimes you might want to load a module irrespective of the route path. Some of those cases are as follows.&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Load on button click without route change.&lt;/li&gt;
&lt;li&gt;Load multiple modules for the current route. In such a case one module can be loaded based on the router &amp;amp; others can be loaded manually.&lt;/li&gt;
&lt;li&gt;Dynamically insert components on some criteria.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;In this post, I am going to show, how to lazily load modules manually in both imperative &amp;amp; declarative (less imperative) ways. Following the declarative way is important when comes to frameworks since it ensures whether the application has less code and is unit testable.&lt;/p&gt;

&lt;p&gt;In terms of frameworks, simply being declarative is leaving the framework to handle things by declaring required syntaxes in the template (as I understand). When I started to learn the differences between imperative and declarative ways, I understand that writing things in the *component.ts file will make you more imperative.&lt;/p&gt;

&lt;p&gt;Let’s get started!!&lt;/p&gt;
&lt;h2&gt;
  
  
  Imperative
&lt;/h2&gt;

&lt;p&gt;The below code loads modules and renders them into the view imperatively.&lt;/p&gt;


&lt;div class="ltag_gist-liquid-tag"&gt;
  
&lt;/div&gt;


&lt;p&gt;The reasons which make this imperative are as follows.&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;The first thing that makes this code imperative is, manually subscribing to the &lt;code&gt;loadModule&lt;/code&gt; method.&lt;/li&gt;
&lt;li&gt;Next, I see we are compiling the module, registering the injector &amp;amp; resolving the component.&lt;/li&gt;
&lt;li&gt;Appending the component in the &lt;code&gt;ng-container&lt;/code&gt; using the &lt;code&gt;ViewContainerRef&lt;/code&gt;.&lt;/li&gt;
&lt;/ul&gt;

&lt;h2&gt;
  
  
  Declarative (Less imperative)
&lt;/h2&gt;

&lt;p&gt;Now let’s see the declarative version of the above component and template file. Also, I would say it is less imperative instead of declarative since I am using some RxJS operators &amp;amp; manually loading the module using &lt;code&gt;load&lt;/code&gt; property.&lt;/p&gt;


&lt;div class="ltag_gist-liquid-tag"&gt;
  
&lt;/div&gt;


&lt;p&gt;Well, I believe you can see the difference here.&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;First thing, lines of code are reduced.&lt;/li&gt;
&lt;li&gt;Removed injection of Compiler.&lt;/li&gt;
&lt;li&gt;Removed manual compiling, injection &amp;amp; component resolving.&lt;/li&gt;
&lt;li&gt;Removed manual appending of the component using ViewContainerRef.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Thanks to Angular!. All the above things comes possible because of the introduction of the &lt;a href="https://angular.io/api/common/NgComponentOutlet"&gt;&lt;code&gt;ngComponentOutletdirective&lt;/code&gt;&lt;/a&gt;. In the above code, I am simply loading the module &amp;amp; resolving the component property after the module loaded successfully.&lt;/p&gt;

&lt;h2&gt;
  
  
  Wrap it up
&lt;/h2&gt;

&lt;p&gt;Out there, I can see there are many blog posts showing different ways to lazy load non-routable modules in angular and you can choose them based on your application scenarios.&lt;br&gt;
You can find the full source code in this &lt;a href="https://github.com/Madhust/LazyLoader"&gt;GitHub&lt;/a&gt; repo. I hope, you have learned something from this blog as I did.&lt;/p&gt;

&lt;p&gt;Thanks for reading. Happy coding. Feel free to contact me if any.&lt;/p&gt;

</description>
      <category>javascript</category>
      <category>webdev</category>
      <category>angular</category>
      <category>programming</category>
    </item>
    <item>
      <title>Conditionally lazy load modules in Angular</title>
      <dc:creator>Madhu Sudhanan P</dc:creator>
      <pubDate>Fri, 22 Apr 2022 07:38:21 +0000</pubDate>
      <link>https://dev.to/madhust/conditionally-lazy-load-modules-in-angular-4l5l</link>
      <guid>https://dev.to/madhust/conditionally-lazy-load-modules-in-angular-4l5l</guid>
      <description>&lt;p&gt;&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fzd7uc5g4ma4ieoq4r3b2.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fzd7uc5g4ma4ieoq4r3b2.png" alt="Image description"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;The lazy loading module is an important feature that helps to improve the performance of an Angular application. This feature is amazing and resolves most of the use cases.&lt;br&gt;
Recently, I faced a user scenario where I need to load a module for a route path conditionally. The scenario is that if a user is assigned with some permission, then load a module or load another module.&lt;br&gt;
The use case seems legit, but I was unable to achieve this using the current lazy loading module feature.&lt;br&gt;
In the current lazy loading feature, we will use loadChildren property to load the required module. The caveat here is that loadChildren doesn’t provide any argument or injectable services which limit us from conditionally loading modules.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;const routes: Routes = [{
       path:'view',
       loadChildren: () =&amp;gt; import('./modules/view/view.module')
                                .then(x =&amp;gt; x.ViewModule)
}];
@NgModule({
   imports: [RouterModule.forRoot(routes, { useHash: true })],
   exports: [RouterModule]
})
export class AppRoutingModule { }
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;To tackle this, I am using the router events to change the loadChildren method definition. We can listen for the RouteConfigLoadStart event which will be triggered before loading the lazy modules.&lt;br&gt;
Since we are configuring the loadChildren method inside the router event handler, we are now provided with injectable services &amp;amp; other options which will provide more control over the module loading.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;import { Component } from '@angular/core';
import { RouteConfigLoadStart, Router } from '@angular/router';
@Component({
 selector: 'my-app',
 templateUrl: './app.component.html',
  styleUrls: ['./app.component.css'],
})
export class AppComponent {
  name = 'Conditionally lazy load modules';
  enableEdit = false;
 constructor(private router: Router) {
  this.router.events.subscribe((x) =&amp;gt; {
    console.log(x);
    if (x instanceof RouteConfigLoadStart &amp;amp;&amp;amp; x.route.path === 'viewedit') {
        x.route.loadChildren = () =&amp;gt; {
          if (!this.enableEdit) {
            return import('./modules/view/view.module').then((mod) =&amp;gt; mod.ViewModule);
   } else {
       return import('./modules/edit/edit.module').then((mod) =&amp;gt; mod.EditModule);
    }
   };
  }
 });
}
. . . 
}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;The complete working sample can be found in the below StackBlitz.&lt;br&gt;
&lt;iframe src="https://stackblitz.com/edit/angular-ivy-rvryyn?" width="100%" height="500"&gt;
&lt;/iframe&gt;
&lt;/p&gt;

&lt;p&gt;Feel free to contact me. Happy coding!!&lt;/p&gt;

</description>
      <category>angular</category>
      <category>javascript</category>
      <category>webdevelopme</category>
      <category>webdev</category>
    </item>
    <item>
      <title>How to publish Blazor WebAssembly application to GitHub pages using GitHub Action</title>
      <dc:creator>Madhu Sudhanan P</dc:creator>
      <pubDate>Mon, 31 May 2021 15:38:10 +0000</pubDate>
      <link>https://dev.to/madhust/how-to-publish-blazor-webassembly-application-to-github-pages-using-github-action-54h4</link>
      <guid>https://dev.to/madhust/how-to-publish-blazor-webassembly-application-to-github-pages-using-github-action-54h4</guid>
      <description>&lt;p&gt;In this post, I am going to show how to add a GitHub Action to your repository that would auto-publish your Blazor WebAssembly application to the GitHub page.&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;This post is going to be focused on creating GitHub Action and publishing the Blazor WebAssembly application. If you are not familiar with creating the Blazor application, you could go through it &lt;a href="https://dotnet.microsoft.com/learn/aspnet/blazor-tutorial/intro" rel="noopener noreferrer"&gt;here&lt;/a&gt;.&lt;/p&gt;
&lt;/blockquote&gt;

&lt;h2&gt;
  
  
  Create GitHub Action workflow
&lt;/h2&gt;

&lt;p&gt;The first step is to create GitHub Action for your Github repository in which your Blazor WebAssembly application resides. You can create the Github Action workflow in the Actions tab of the repository.&lt;br&gt;
&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Ftqhsgb3t2t5tnp5dkz46.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Ftqhsgb3t2t5tnp5dkz46.png" alt="Create GitHub actions"&gt;&lt;/a&gt;&lt;br&gt;
Now a YAML file will be created, name the file as per your wish.&lt;br&gt;
&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2F96fvhrwtrbq45aorxei2.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2F96fvhrwtrbq45aorxei2.png" alt="Template"&gt;&lt;/a&gt;&lt;br&gt;
In the YAML file, we are going to provide the below workflow steps to build and publish the WASM application to GitHub pages.&lt;/p&gt;
&lt;h2&gt;
  
  
  Set up
&lt;/h2&gt;

&lt;p&gt;Now we are going to do the initial setup. In the initial setup, I am providing the name and setting an environment variable to use inside other workflow steps.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight yaml"&gt;&lt;code&gt;&lt;span class="na"&gt;name&lt;/span&gt;&lt;span class="pi"&gt;:&lt;/span&gt; &lt;span class="s"&gt;Deploy Blazor WASM to GitHub Page&lt;/span&gt;
&lt;span class="na"&gt;env&lt;/span&gt;&lt;span class="pi"&gt;:&lt;/span&gt;
  &lt;span class="na"&gt;PUBLISH_DIR&lt;/span&gt;&lt;span class="pi"&gt;:&lt;/span&gt; &lt;span class="s"&gt;bin/Release/net5.0/publish/wwwroot&lt;/span&gt;

&lt;span class="c1"&gt;# Controls when the action will run&lt;/span&gt;
&lt;span class="na"&gt;on&lt;/span&gt;&lt;span class="pi"&gt;:&lt;/span&gt;
  &lt;span class="na"&gt;push&lt;/span&gt;&lt;span class="pi"&gt;:&lt;/span&gt;
    &lt;span class="na"&gt;branches&lt;/span&gt;&lt;span class="pi"&gt;:&lt;/span&gt; &lt;span class="pi"&gt;[&lt;/span&gt; &lt;span class="nv"&gt;master&lt;/span&gt; &lt;span class="pi"&gt;]&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;


&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;name&lt;/strong&gt;: Name of the GitHub action workflow&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;env -&amp;gt; PUBLISH_DIR&lt;/strong&gt;: Denotes the folder in which our application will be published during publish command.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;on – &amp;gt; push –&amp;gt; branches&lt;/strong&gt;: Specifies that the workflow steps should be executed when pushed to the main branch. &lt;/li&gt;
&lt;/ul&gt;
&lt;h2&gt;
  
  
  Publish the application
&lt;/h2&gt;

&lt;p&gt;The next workflow step is to publish the application. It can be done using the below code.&lt;br&gt;
&lt;/p&gt;
&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight yaml"&gt;&lt;code&gt;&lt;span class="na"&gt;jobs&lt;/span&gt;&lt;span class="pi"&gt;:&lt;/span&gt;
  &lt;span class="na"&gt;build&lt;/span&gt;&lt;span class="pi"&gt;:&lt;/span&gt;
    &lt;span class="na"&gt;runs-on&lt;/span&gt;&lt;span class="pi"&gt;:&lt;/span&gt; &lt;span class="s"&gt;ubuntu-latest&lt;/span&gt;
    &lt;span class="na"&gt;steps&lt;/span&gt;&lt;span class="pi"&gt;:&lt;/span&gt;
    &lt;span class="pi"&gt;-&lt;/span&gt; &lt;span class="na"&gt;uses&lt;/span&gt;&lt;span class="pi"&gt;:&lt;/span&gt; &lt;span class="s"&gt;actions/checkout@v2&lt;/span&gt;

    &lt;span class="pi"&gt;-&lt;/span&gt; &lt;span class="na"&gt;name&lt;/span&gt;&lt;span class="pi"&gt;:&lt;/span&gt; &lt;span class="s"&gt;Publish application&lt;/span&gt;
      &lt;span class="na"&gt;run&lt;/span&gt;&lt;span class="pi"&gt;:&lt;/span&gt; &lt;span class="s"&gt;dotnet publish -c Release&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;


&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;name&lt;/strong&gt;: Name of the workflow step.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;run&lt;/strong&gt;: Runs any shell command. We are publishing our Blazor application in release mode here.&lt;/li&gt;
&lt;/ul&gt;
&lt;h2&gt;
  
  
  Rewrite the base element's href attribute
&lt;/h2&gt;

&lt;p&gt;In the published application, the base element's href value is set to *&lt;em&gt;*&lt;/em&gt;. Since the GitHub page is published and served under our repository name, the repository name should be given as base element's href value. It can be done using a simple GitHub Action written by &lt;a href="https://github.com/SteveSandersonMS" rel="noopener noreferrer"&gt;SteveSanderson&lt;/a&gt;.&lt;br&gt;
&lt;/p&gt;
&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight yaml"&gt;&lt;code&gt;    &lt;span class="c1"&gt;# base href url value should be changed so that resources like CSS and scripts can load properly. &lt;/span&gt;
    &lt;span class="pi"&gt;-&lt;/span&gt; &lt;span class="na"&gt;name&lt;/span&gt;&lt;span class="pi"&gt;:&lt;/span&gt; &lt;span class="s"&gt;Rewrite base href&lt;/span&gt;
      &lt;span class="na"&gt;if&lt;/span&gt;&lt;span class="pi"&gt;:&lt;/span&gt; &lt;span class="s"&gt;success()&lt;/span&gt;
      &lt;span class="na"&gt;uses&lt;/span&gt;&lt;span class="pi"&gt;:&lt;/span&gt; &lt;span class="s"&gt;SteveSandersonMS/ghaction-rewrite-base-href@v1&lt;/span&gt;
      &lt;span class="na"&gt;with&lt;/span&gt;&lt;span class="pi"&gt;:&lt;/span&gt;
        &lt;span class="na"&gt;html_path&lt;/span&gt;&lt;span class="pi"&gt;:&lt;/span&gt; &lt;span class="s"&gt;${{ env.PUBLISH_DIR }}/index.html&lt;/span&gt;
        &lt;span class="na"&gt;base_href&lt;/span&gt;&lt;span class="pi"&gt;:&lt;/span&gt; &lt;span class="s"&gt;REPOSITORY_NAME&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;


&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;name&lt;/strong&gt;: Name of the step.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;if&lt;/strong&gt;: Continue if only the above workflow steps passed.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;uses&lt;/strong&gt;: Use GitHub Action by &lt;a href="https://github.com/SteveSandersonMS" rel="noopener noreferrer"&gt;SteveSanderson&lt;/a&gt; to rewrite the base URL. It will be called with two parameters. 

&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;html_path&lt;/strong&gt;: Path of the index.html file&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;base_href&lt;/strong&gt;: Replacement value for base href attribute.&lt;/li&gt;
&lt;/ul&gt;
&lt;/li&gt;
&lt;/ul&gt;
&lt;h2&gt;
  
  
  Add .nojekyll file
&lt;/h2&gt;

&lt;p&gt;With the base href value is re-written, we are all set to push the published folder to the GitHub page. But there is another stumble that needs to be addressed. GitHub pages don’t serve files from folders starting with an underscore(_).&lt;/p&gt;

&lt;p&gt;So the files under the folder &lt;strong&gt;_framework&lt;/strong&gt; and &lt;strong&gt;_content&lt;/strong&gt; in our published folder will not served by the GitHub pages and the application will break. To resolve this problem we need to add the &lt;em&gt;.nojekyll&lt;/em&gt; file to our folder using the below step.&lt;br&gt;
&lt;/p&gt;
&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight yaml"&gt;&lt;code&gt;    &lt;span class="c1"&gt;# add .nojekyll file to tell GitHub pages to not treat this as a Jekyll project. &lt;/span&gt;
   &lt;span class="c1"&gt;# Allow files and folders starting with an underscore.&lt;/span&gt;
    &lt;span class="pi"&gt;-&lt;/span&gt; &lt;span class="na"&gt;name&lt;/span&gt;&lt;span class="pi"&gt;:&lt;/span&gt; &lt;span class="s"&gt;Add .nojekyll file&lt;/span&gt;
      &lt;span class="na"&gt;run&lt;/span&gt;&lt;span class="pi"&gt;:&lt;/span&gt; &lt;span class="s"&gt;touch ${{ env.PUBLISH_DIR }}/.nojekyll&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;

&lt;h2&gt;
  
  
  Commit to GitHub page branch
&lt;/h2&gt;

&lt;p&gt;Now push the published directory to the GitHub page branch using the below workflow step.&lt;br&gt;
&lt;/p&gt;
&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight yaml"&gt;&lt;code&gt;    &lt;span class="pi"&gt;-&lt;/span&gt; &lt;span class="na"&gt;name&lt;/span&gt;&lt;span class="pi"&gt;:&lt;/span&gt; &lt;span class="s"&gt;Commit to GitHub pages Repo&lt;/span&gt;
      &lt;span class="na"&gt;if&lt;/span&gt;&lt;span class="pi"&gt;:&lt;/span&gt; &lt;span class="s"&gt;success()&lt;/span&gt;
      &lt;span class="na"&gt;uses&lt;/span&gt;&lt;span class="pi"&gt;:&lt;/span&gt; &lt;span class="s"&gt;crazy-max/ghaction-github-pages@v1.5.1&lt;/span&gt;
      &lt;span class="na"&gt;with&lt;/span&gt;&lt;span class="pi"&gt;:&lt;/span&gt;
        &lt;span class="na"&gt;target_branch&lt;/span&gt;&lt;span class="pi"&gt;:&lt;/span&gt; &lt;span class="s"&gt;gh-pages&lt;/span&gt;
        &lt;span class="na"&gt;build_dir&lt;/span&gt;&lt;span class="pi"&gt;:&lt;/span&gt; &lt;span class="s"&gt;${{ env.PUBLISH_DIR }}&lt;/span&gt;
      &lt;span class="na"&gt;env&lt;/span&gt;&lt;span class="pi"&gt;:&lt;/span&gt;
        &lt;span class="na"&gt;GITHUB_TOKEN&lt;/span&gt;&lt;span class="pi"&gt;:&lt;/span&gt; &lt;span class="s"&gt;${{ secrets.GITHUB_TOKEN }}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;


&lt;p&gt;You can see that the &lt;strong&gt;target_branch&lt;/strong&gt; parameter is set to &lt;strong&gt;gh-pages&lt;/strong&gt; so we need to set the GitHub pages source branch as &lt;strong&gt;gh-pages&lt;/strong&gt; in the &lt;strong&gt;Settings -&amp;gt; Pages&lt;/strong&gt; section.&lt;br&gt;
&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fjd4drgbi4rxvnp1g1uuf.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fjd4drgbi4rxvnp1g1uuf.png" alt="Alt Text"&gt;&lt;/a&gt;&lt;br&gt;
The full GitHub Action workflow steps are as follows.&lt;br&gt;
&lt;/p&gt;
&lt;div class="ltag_gist-liquid-tag"&gt;
  
&lt;/div&gt;



&lt;blockquote&gt;
&lt;p&gt;In case, you want to do auto deploy your app then I suggest you to check out &lt;a href="https://www.nuget.org/packages/PublishSPAforGitHubPages.Build/" rel="noopener noreferrer"&gt;PublishSPAforGitHubPages.Build&lt;/a&gt; nuget package, which performs post publish process to deploy your application to GitHub pages. &lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;Thanks for reading. Happy coding!!&lt;/p&gt;

</description>
      <category>blazor</category>
      <category>dotnet</category>
      <category>githubactions</category>
      <category>aspnetcore</category>
    </item>
    <item>
      <title>NDepend – a cutting edge static code analysis tool</title>
      <dc:creator>Madhu Sudhanan P</dc:creator>
      <pubDate>Sun, 30 May 2021 08:50:15 +0000</pubDate>
      <link>https://dev.to/madhust/ndepend-a-cutting-edge-static-code-analysis-tool-2g03</link>
      <guid>https://dev.to/madhust/ndepend-a-cutting-edge-static-code-analysis-tool-2g03</guid>
      <description>&lt;p&gt;Writing code is fun and but writing maintainable code is a challenge and it is much needed for large-scale projects.  Manually validating the code quality, maintaining performance and other factors are not feasible and will not help in the long term. This is one of the reasons, the developers are using static code analyzer tools when working with maintaining a large codebase.&lt;/p&gt;

&lt;p&gt;I am going to discuss such a static analyzing tool which impressed me. And its many features make code maintainability a piece of cake.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://www.ndepend.com/"&gt;NDepend&lt;/a&gt;, a static analysis tool for .NET developers. It is well designed, easy to use, and provides deep insight into your codebase to reduce future issues.&lt;/p&gt;

&lt;h2&gt;
  
  
  Download and Installation
&lt;/h2&gt;

&lt;p&gt;You can download the NDepend from &lt;a href="https://www.ndepend.com/purchase"&gt;here&lt;/a&gt;. It provides a 14-days trial period with three setup variations to choose from.&lt;/p&gt;

&lt;h2&gt;
  
  
  Maintain code using Code rules and Quality gates
&lt;/h2&gt;

&lt;p&gt;Quality gates feature helps the product owner or scrum master to daily report on the project status and give insight on the product releases. Many companies are now adopting an agile framework and continuous integration, which enables them to have a deliverable product for each commit and allows companies to have short-term releases. In such environments maintaining code standard is more difficult and manual checking will not help on a large scale and makes work tedious.&lt;br&gt;&lt;br&gt;
NDepend provides a vast set of code rules and quality gates which ensures that each commit abides necessary set of rules before committed in the main codebase.&lt;br&gt;
The quality gate violation or code rule violations could be easily visualized in the interactive dashboard provided by NDepend. An example dashboard with quality gate failure is as follows.&lt;br&gt;&lt;br&gt;
&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--I3cldc0T--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/2dfd7hsb4up79o07w9vm.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--I3cldc0T--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/2dfd7hsb4up79o07w9vm.png" alt="Quality Gate"&gt;&lt;/a&gt;&lt;br&gt;
The violations can be viewed in the &lt;strong&gt;Queries and Rules Explorer&lt;/strong&gt;. The two Quality gates violations are shown in the below image.&lt;br&gt;
&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--I-J4vINZ--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/qbv87suosnyq5t5x8gh9.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--I-J4vINZ--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/qbv87suosnyq5t5x8gh9.png" alt="Alt Text"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;h2&gt;
  
  
  Code review
&lt;/h2&gt;

&lt;p&gt;Code review is an important phase in the software development lifecycle. It brings a collaboration of peer developers to review your code to make it better.  The role of the NDepend static analyzer tool is inevitable since it reduces many manual works by ensuring various rules.&lt;/p&gt;

&lt;p&gt;As a scrum master, I think the following queries of the NDepend must be followed during code review for a smooth and successful release. &lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Find and fix Quality gates violations.&lt;/li&gt;
&lt;li&gt;Regression issues – Regression issues make the end-user frustrated and reduce trust against the product/service.&lt;/li&gt;
&lt;li&gt;API breaking changes – Users don’t like frequent changes in APIs as it would bring rework and time-consuming.&lt;/li&gt;
&lt;li&gt;Dead Code – Nowadays, apps should load and work at high speed, removing dead code would reduce the final product size. &lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;There are many other queries available, these are my own minimal list of queries recommendations to be followed during code review.&lt;/p&gt;

&lt;p&gt;Another impressive feature in NDepend is its in-built Diff viewer of the code against its baseline. This helps to view the code changes without leaving the visual studio during code review phase.&lt;/p&gt;

&lt;h2&gt;
  
  
  Architecture diagram
&lt;/h2&gt;

&lt;p&gt;NDepend allows you to visualize application architecture and dependencies using Dependencies Graph and Dependencies Matrix diagrams.&lt;/p&gt;

&lt;p&gt;This help us to understand the coupling between projects. The dependencies are represented by thousands of elements that helps to understand legacy code and its dependencies. I tried this architecture diagram on &lt;a href="https://github.com/blazorhero/CleanArchitecture"&gt;Blazor Clean Architecture&lt;/a&gt; which is as follows.&lt;br&gt;
&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--5UhQ-bYA--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/gsqk99ovrwf9pfe82e7w.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--5UhQ-bYA--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/gsqk99ovrwf9pfe82e7w.png" alt="Quality Graph"&gt;&lt;/a&gt;&lt;br&gt;
&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--7F_cG4oY--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/fbjz0mg0eajfph2aabki.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--7F_cG4oY--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/fbjz0mg0eajfph2aabki.png" alt="Quality Matrix"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;h2&gt;
  
  
  Reporting
&lt;/h2&gt;

&lt;p&gt;NDepend also provides a customizable Report of the code base analysis in HTML format. The report will contain all analysis details of the code base such as quality gate violations, rules violations including architecture diagrams. The HTML page has an interactive UI that allows you to click and view violations within the report.&lt;br&gt;&lt;br&gt;
&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--qCDdKdIV--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/ua5vw8xu9ogs99cqai7x.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--qCDdKdIV--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/ua5vw8xu9ogs99cqai7x.png" alt="Alt Text"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--WIkWjGZ---/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/5c14abw6ygbe63h2ffb3.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--WIkWjGZ---/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/5c14abw6ygbe63h2ffb3.png" alt="Alt Text"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--pZYnSSAF--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/500m6sv64l906sr0ltk6.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--pZYnSSAF--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/500m6sv64l906sr0ltk6.png" alt="Alt Text"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Also, the report contains a set of diagrams for our codebase. You can click the required diagram to view it on full screen.  For architecture diagrams, I find it easier to use in the visual studio than the HTML report.&lt;br&gt;
&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--Zz5NaTeP--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/z05gt12zvisrvcjjsd98.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--Zz5NaTeP--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/z05gt12zvisrvcjjsd98.png" alt="Alt Text"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;h2&gt;
  
  
  Summary
&lt;/h2&gt;

&lt;p&gt;&lt;a href="https://www.ndepend.com/"&gt;NDepend&lt;/a&gt; has a large set of features that help us in various scenarios. I just only covered some of the features I used till today. &lt;/p&gt;

&lt;p&gt;If you are looking for a static analysis tool with advanced features and reporting system, then you should give NDepend a try. It is more than a static analysis tool, it’s a &lt;strong&gt;”Swiss Army Knife”&lt;/strong&gt; for .NET Developers.&lt;/p&gt;

&lt;p&gt;Thanks for reading. Happy coding!!&lt;/p&gt;

&lt;h2&gt;
  
  
  References
&lt;/h2&gt;

&lt;p&gt;&lt;a href="https://www.ndepend.com/docs/getting-started-with-ndepend"&gt;https://www.ndepend.com/docs/getting-started-with-ndepend&lt;/a&gt;&lt;br&gt;
&lt;a href="https://github.com/blazorhero/CleanArchitecture"&gt;https://github.com/blazorhero/CleanArchitecture&lt;/a&gt;&lt;/p&gt;

</description>
      <category>dotnet</category>
      <category>blazor</category>
      <category>codequality</category>
      <category>ndepend</category>
    </item>
    <item>
      <title>Blazor in depth — Create Blazor component without Lifecycle methods</title>
      <dc:creator>Madhu Sudhanan P</dc:creator>
      <pubDate>Fri, 16 Oct 2020 17:20:16 +0000</pubDate>
      <link>https://dev.to/madhust/blazor-in-depth-create-blazor-component-without-lifecycle-methods-2786</link>
      <guid>https://dev.to/madhust/blazor-in-depth-create-blazor-component-without-lifecycle-methods-2786</guid>
      <description>&lt;p&gt;It has been long time since I have managed to write a blog post. As I got up with a very challenging Blazor project in workplace, I got no time to share my thought with the community. Glad to be back!!.&lt;/p&gt;

&lt;p&gt;In this blog post, I am going to share how I created a Blazor component without life-cycle methods and razor file syntax. We could also say this as class only component approach but without life-cycle methods. Many of them might knew this way of component rendering, but I felt like sharing this would help other folks who doesn’t know. I found this approach after going through various components used in Blazor source code.&lt;/p&gt;

&lt;p&gt;To demonstrate this approach, we are going to create a simple Rating component.&lt;/p&gt;

&lt;p&gt;I am creating a class named Rating.cs. As mentioned previously this is a class only component so I am not creating Razor component item.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight csharp"&gt;&lt;code&gt;&lt;span class="k"&gt;public&lt;/span&gt; &lt;span class="k"&gt;class&lt;/span&gt; &lt;span class="nc"&gt;Rating&lt;/span&gt;
&lt;span class="p"&gt;{&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;First step in this approach is to implement Rating class with IComponent interface.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight csharp"&gt;&lt;code&gt;&lt;span class="k"&gt;public&lt;/span&gt; &lt;span class="k"&gt;class&lt;/span&gt; &lt;span class="nc"&gt;Rating&lt;/span&gt; &lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="n"&gt;IComponent&lt;/span&gt;
&lt;span class="p"&gt;{&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Now the Rating class must implement below two methods of IComponent interface.&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Attach — Attach the RenderHandle instance to the rating component.&lt;/li&gt;
&lt;li&gt;SetParametersAsync — Set the component parameters. Yes I lied before, we need this life cycle method. But I promise, this is the only one needed I will not add no more.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;In the Attach method, we need to get and store the RenderHandle instance for later usage. Now the code will look like below.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight csharp"&gt;&lt;code&gt;&lt;span class="k"&gt;public&lt;/span&gt; &lt;span class="k"&gt;class&lt;/span&gt; &lt;span class="nc"&gt;Rating&lt;/span&gt; &lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="n"&gt;IComponent&lt;/span&gt;
&lt;span class="p"&gt;{&lt;/span&gt;
     &lt;span class="k"&gt;private&lt;/span&gt; &lt;span class="n"&gt;RenderHandle&lt;/span&gt; &lt;span class="n"&gt;_renderHandle&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;     

     &lt;span class="k"&gt;void&lt;/span&gt; &lt;span class="n"&gt;IComponent&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;Attach&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;RenderHandle&lt;/span&gt; &lt;span class="n"&gt;renderHandle&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
     &lt;span class="p"&gt;{&lt;/span&gt;
        &lt;span class="n"&gt;_renderHandle&lt;/span&gt; &lt;span class="p"&gt;=&lt;/span&gt; &lt;span class="n"&gt;renderHandle&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
     &lt;span class="p"&gt;}&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Now we need to handle the SetParametersAsync method, this method should take care of below two important responsibilities.&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Get component parameters from ParameterView and set in the corresponding component property. Here Rating component uses Value property.&lt;/li&gt;
&lt;li&gt;Invoke Render method of the RenderHandle with the RenderFragment delegate.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Now the final rating component will look like below.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight csharp"&gt;&lt;code&gt;    &lt;span class="k"&gt;public&lt;/span&gt; &lt;span class="k"&gt;class&lt;/span&gt; &lt;span class="nc"&gt;Rating&lt;/span&gt; &lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="n"&gt;IComponent&lt;/span&gt;
    &lt;span class="p"&gt;{&lt;/span&gt;
        &lt;span class="k"&gt;private&lt;/span&gt; &lt;span class="n"&gt;RenderHandle&lt;/span&gt; &lt;span class="n"&gt;_renderHandle&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;        

        &lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="n"&gt;Parameter&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt;
        &lt;span class="k"&gt;public&lt;/span&gt; &lt;span class="kt"&gt;int&lt;/span&gt; &lt;span class="n"&gt;Value&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="k"&gt;get&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt; &lt;span class="k"&gt;set&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt; &lt;span class="p"&gt;}&lt;/span&gt;        

        &lt;span class="k"&gt;void&lt;/span&gt; &lt;span class="n"&gt;IComponent&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;Attach&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;RenderHandle&lt;/span&gt; &lt;span class="n"&gt;renderHandle&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
        &lt;span class="p"&gt;{&lt;/span&gt;
            &lt;span class="n"&gt;_renderHandle&lt;/span&gt; &lt;span class="p"&gt;=&lt;/span&gt; &lt;span class="n"&gt;renderHandle&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
        &lt;span class="p"&gt;}&lt;/span&gt;

        &lt;span class="n"&gt;Task&lt;/span&gt; &lt;span class="n"&gt;IComponent&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;SetParametersAsync&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;ParameterView&lt;/span&gt; &lt;span class="n"&gt;parameters&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
        &lt;span class="p"&gt;{&lt;/span&gt;
            &lt;span class="k"&gt;foreach&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="kt"&gt;var&lt;/span&gt; &lt;span class="n"&gt;entry&lt;/span&gt; &lt;span class="k"&gt;in&lt;/span&gt; &lt;span class="n"&gt;parameters&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
            &lt;span class="p"&gt;{&lt;/span&gt;
                &lt;span class="k"&gt;switch&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;entry&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;Name&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
                &lt;span class="p"&gt;{&lt;/span&gt;
                    &lt;span class="k"&gt;case&lt;/span&gt; &lt;span class="k"&gt;nameof&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;Value&lt;/span&gt;&lt;span class="p"&gt;):&lt;/span&gt;
                        &lt;span class="n"&gt;Value&lt;/span&gt; &lt;span class="p"&gt;=&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="kt"&gt;int&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="n"&gt;entry&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;Value&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
                        &lt;span class="k"&gt;break&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
                &lt;span class="p"&gt;}&lt;/span&gt;
            &lt;span class="p"&gt;}&lt;/span&gt;
            &lt;span class="n"&gt;_renderHandle&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;Render&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;RenderDelegate&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;                         
            &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="n"&gt;Task&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;CompletedTask&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
        &lt;span class="p"&gt;}&lt;/span&gt;        

        &lt;span class="k"&gt;private&lt;/span&gt; &lt;span class="k"&gt;void&lt;/span&gt; &lt;span class="nf"&gt;RenderDelegate&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;RenderTreeBuilder&lt;/span&gt; &lt;span class="n"&gt;builder&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
        &lt;span class="p"&gt;{&lt;/span&gt;
            &lt;span class="kt"&gt;int&lt;/span&gt; &lt;span class="n"&gt;max&lt;/span&gt; &lt;span class="p"&gt;=&lt;/span&gt; &lt;span class="n"&gt;Math&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;Min&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;Value&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="m"&gt;5&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
            &lt;span class="kt"&gt;int&lt;/span&gt; &lt;span class="n"&gt;seq&lt;/span&gt; &lt;span class="p"&gt;=&lt;/span&gt; &lt;span class="m"&gt;1&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
            &lt;span class="k"&gt;for&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="kt"&gt;var&lt;/span&gt; &lt;span class="n"&gt;i&lt;/span&gt; &lt;span class="p"&gt;=&lt;/span&gt; &lt;span class="m"&gt;0&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt; &lt;span class="n"&gt;i&lt;/span&gt; &lt;span class="p"&gt;&amp;lt;&lt;/span&gt; &lt;span class="n"&gt;max&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt; &lt;span class="n"&gt;i&lt;/span&gt;&lt;span class="p"&gt;++)&lt;/span&gt;
            &lt;span class="p"&gt;{&lt;/span&gt;
                &lt;span class="n"&gt;builder&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;OpenElement&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;seq&lt;/span&gt;&lt;span class="p"&gt;++,&lt;/span&gt; &lt;span class="s"&gt;"span"&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
                &lt;span class="n"&gt;builder&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;AddAttribute&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;seq&lt;/span&gt;&lt;span class="p"&gt;++,&lt;/span&gt; 
                          &lt;span class="s"&gt;"style"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="s"&gt;"color:#f49813;font-size:30px"&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
                &lt;span class="n"&gt;builder&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;AddContent&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;seq&lt;/span&gt;&lt;span class="p"&gt;++,&lt;/span&gt; &lt;span class="s"&gt;"★"&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
                &lt;span class="n"&gt;builder&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;CloseElement&lt;/span&gt;&lt;span class="p"&gt;();&lt;/span&gt;
            &lt;span class="p"&gt;}&lt;/span&gt;
        &lt;span class="p"&gt;}&lt;/span&gt;
    &lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Now the Rating component can be used in our application simply like below.&lt;br&gt;
&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fi%2F41by4uq0w3yd8b3rs1p1.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fi%2F41by4uq0w3yd8b3rs1p1.png" alt="Alt Text"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;The output looks like below.&lt;br&gt;
&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fi%2Fncui25z14ss9lonao3nl.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fi%2Fncui25z14ss9lonao3nl.png" alt="Alt Text"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;h3&gt;
  
  
  Closing
&lt;/h3&gt;

&lt;p&gt;I hope you have learned one of many ways of creating Blazor components. In this post, I am neither recommending this as a best approach nor insisting to use this approach for performance benefits. I found this approach as a simple and clean way of creating simple Blazor components.&lt;/p&gt;

&lt;p&gt;Leave comments or share this post if you found this helpful. Happy coding !!&lt;/p&gt;

</description>
      <category>dotnet</category>
      <category>blazor</category>
      <category>aspnetcore</category>
      <category>dotnet5</category>
    </item>
  </channel>
</rss>
