<?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>Forem: Drew Payment</title>
    <description>The latest articles on Forem by Drew Payment (@drewpayment).</description>
    <link>https://forem.com/drewpayment</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%2F364497%2F6eb57919-f9ed-442d-8aa0-c122d3e81c0b.jpeg</url>
      <title>Forem: Drew Payment</title>
      <link>https://forem.com/drewpayment</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://forem.com/feed/drewpayment"/>
    <language>en</language>
    <item>
      <title>Angular's Change Detection</title>
      <dc:creator>Drew Payment</dc:creator>
      <pubDate>Wed, 15 Apr 2020 12:43:37 +0000</pubDate>
      <link>https://forem.com/drewpayment/angular-s-change-detection-58ip</link>
      <guid>https://forem.com/drewpayment/angular-s-change-detection-58ip</guid>
      <description>&lt;p&gt;During the Angular lifecycle, all components are susceptible to Angular's incredible change detection system. Any time something changes in your app, Angular makes sure to run through it's change detection (which is extremely performant all things considered) but this process can become a bottleneck in complex web applications. Fear not! There is a way to circumvent the normal change detection where Angular will review every single element in it's reaches, potentially bogging down your web app. &lt;/p&gt;

&lt;p&gt;In your component decorator:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight plaintext"&gt;&lt;code&gt;@Component({
  selector: 'app-my-component',
  templateUrl: './my-component.component.html',
  changeDetection: ChangeDetectionStrategy.OnPush
})
export class MyComponentComponent {
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;p&gt;By updating your change detection strategy, you've effectively told Angular that you don't want external changes to queue your component up for Angular to take a look at it. &lt;/p&gt;

&lt;p&gt;For example, let's assume you were making the famous todo list app.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight plaintext"&gt;&lt;code&gt;@Component({
  selector: 'app-todo-list',
  templateUrl: './todo-list.component.html',
  changeDetection: ChangeDetectionStrategy.OnPush
})
export class TodoListComponent {

  todos = ['Read Drew's post.', 'Review the post'];

  addTodo(todo) {
    this.todos.push(todo);
  }

}

/** and this is your template (todo-list.component.html) **/

&amp;lt;input #newTodo type="text" placeholder="Enter a todo"&amp;gt;
&amp;lt;button type="button" (click)="addTodo(newTodo.value)"&amp;gt;Add Todo&amp;lt;/button&amp;gt;

&amp;lt;ul&amp;gt;
  &amp;lt;li *ngFor="let todo of todos"&amp;gt;{{ todo }}&amp;lt;/li&amp;gt;
&amp;lt;/ul&amp;gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;p&gt;With the default change detection (ChangeDetectionStrategy.Default), you would expect adding a todo to update the list. &lt;/p&gt;

&lt;p&gt;However, what happens above in our scenario where we've used &lt;code&gt;OnPush&lt;/code&gt; for our change detection strategy? You might be surprised to learn that it doesn't update the list in our DOM, although we could &lt;code&gt;console.log(todos)&lt;/code&gt; and see that it definitely added the our new todo to that list. &lt;/p&gt;

&lt;p&gt;The fix? Rxjs. We need to show Angular a new reference to a new array in that list on the DOM for it to change the view. Like this:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight plaintext"&gt;&lt;code&gt;@Component({
  selector: 'app-todo-list',
  templateUrl: './todo-list.component.html',
  changeDetection: ChangeDetectionStrategy.OnPush
})
export class TodoListComponent {

  private todos = ['Read Drew's post.', 'Review the post'];
  todos$ = new BehaviorSubject&amp;lt;string[]&amp;gt;(todos);

  addTodo(todo) {
    this.todos.push(todo);
    this.todos$.next(this.todos);
  }

}

/** and this is your template (todo-list.component.html) **/

&amp;lt;input #newTodo type="text" placeholder="Enter a todo"&amp;gt;
&amp;lt;button type="button" (click)="addTodo(newTodo.value)"&amp;gt;Add Todo&amp;lt;/button&amp;gt;

&amp;lt;ul&amp;gt;
  &amp;lt;ng-container *ngFor="let todo of todos$|async"&amp;gt;
    &amp;lt;li&amp;gt;{{ todo }}&amp;lt;/li&amp;gt;
  &amp;lt;/ng-container&amp;gt;
&amp;lt;/ul&amp;gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;p&gt;What happens if you just can't get something to update still? Angular enables us the ability to tap into the change detection system and to tell Angular explicitly when to update:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight plaintext"&gt;&lt;code&gt;@Component({
  selector: 'app-todo-list',
  templateUrl: './todo-list.component.html',
  changeDetection: ChangeDetectionStrategy.OnPush
})
export class TodoListComponent {

  private todos = ['Read Drew's post.', 'Review the post'];

  constructor(private cd: ChangeDetectorRef) {}

  addTodo(todo) {
    this.todos.push(todo);
    this.cd.detechChanges();
  }

}
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;p&gt;This would do essentially the same thing as our first example. Although it is still forcing Angular to run change detection which is just doing the same thing we were trying to avoid by using the &lt;code&gt;OnPush&lt;/code&gt; change detection strategy in the first place. So, only use it when absolutely necessary.&lt;/p&gt;

</description>
      <category>angular</category>
      <category>javascript</category>
    </item>
    <item>
      <title>Covid-19 Angular Web App</title>
      <dc:creator>Drew Payment</dc:creator>
      <pubDate>Sun, 12 Apr 2020 11:00:11 +0000</pubDate>
      <link>https://forem.com/drewpayment/covid-19-angular-web-app-44d9</link>
      <guid>https://forem.com/drewpayment/covid-19-angular-web-app-44d9</guid>
      <description>&lt;p&gt;I wrote an Angular web app using mostly the Material library, Plotly, Highcharts, and Highmaps. It brings together some free design and creative resources and uses data available from WHO, CDC and John Hopkins through varied web scrapers and APIs found on GitHub. &lt;/p&gt;

&lt;p&gt;Please share any feedback and let me know what you think I could do to improve it. I would like to incorporate TF, but not sure how to use any ML with the datasets available. &lt;/p&gt;

&lt;p&gt;&lt;a href="Https://stopcovid.dev"&gt;Https://stopcovid.dev&lt;/a&gt;&lt;/p&gt;

</description>
      <category>angular</category>
      <category>webdev</category>
      <category>design</category>
    </item>
  </channel>
</rss>
