<?xml version="1.0" encoding="UTF-8"?><rss xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:content="http://purl.org/rss/1.0/modules/content/" xmlns:atom="http://www.w3.org/2005/Atom" version="2.0" xmlns:cc="http://cyber.law.harvard.edu/rss/creativeCommonsRssModule.html">
    <channel>
        <title><![CDATA[Stories by kingsley ezeokeke on Medium]]></title>
        <description><![CDATA[Stories by kingsley ezeokeke on Medium]]></description>
        <link>https://medium.com/@kuebano?source=rss-bcdf37e6de30------2</link>
        <image>
            <url>https://cdn-images-1.medium.com/fit/c/150/150/1*xjpdj7SKpVJQotW9xknuLw.png</url>
            <title>Stories by kingsley ezeokeke on Medium</title>
            <link>https://medium.com/@kuebano?source=rss-bcdf37e6de30------2</link>
        </image>
        <generator>Medium</generator>
        <lastBuildDate>Wed, 02 Dec 2026 00:35:12 GMT</lastBuildDate>
        <atom:link href="https://medium.com/@kuebano/feed" rel="self" type="application/rss+xml"/>
        <webMaster><![CDATA[yourfriends@medium.com]]></webMaster>
        <atom:link href="http://medium.superfeedr.com/" rel="hub"/>
        <item>
            <title><![CDATA[Look at it from my ANGLE (An Angular series) — The Life-cycle of an Angular component]]></title>
            <link>https://medium.com/@kuebano/look-at-it-from-my-angle-an-angular-series-the-life-cycle-of-an-angular-component-484a41a02bd0?source=rss-bcdf37e6de30------2</link>
            <guid isPermaLink="false">https://medium.com/p/484a41a02bd0</guid>
            <category><![CDATA[software-engineering]]></category>
            <category><![CDATA[angular]]></category>
            <category><![CDATA[software-development]]></category>
            <category><![CDATA[javascript]]></category>
            <category><![CDATA[typescript]]></category>
            <dc:creator><![CDATA[kingsley ezeokeke]]></dc:creator>
            <pubDate>Fri, 08 Jun 2018 19:05:09 GMT</pubDate>
            <atom:updated>2018-06-08T19:05:09.872Z</atom:updated>
            <content:encoded><![CDATA[<h4>What you need to know about the different phases of an angular component’s life-cycle and best practices</h4><figure><img alt="" src="https://cdn-images-1.medium.com/max/385/1*ZBE4K_Vpp17GUPh5fl0VXQ.png" /></figure><p>The life-cycle of an Angular component simply means the phases that occurs from when a component is created till when it is destroyed.</p><p>This is a very important aspect when dealing with components in Angular.</p><p>A good knowledge of the various phases will help us to gain more control of our application.</p><h3>Creation</h3><p>The creation of a component starts with the <strong>constructor</strong> method. This method is the starting point of every component. Also, Services can only be injected into a component as parameters in the constructor method.</p><pre>export class AppComponent {</pre><pre>    constructor() {}</pre><pre>}</pre><h3>Sequence</h3><p>Now the component has been created, what next? I will be explaining the life-cycle hooks in the manner in which they are triggered.</p><p><strong>ngOnChanges() : </strong>This Hook is triggered immediately after the <em>constructor</em>, and any time there is a change in the <a href="https://angular.io/guide/component-interaction">input properties</a> of a component.</p><p><strong>ngOnInit() : </strong>This Hook is triggered after the first <em>ngOnChanges. It is only triggered once in the entire life-cycle.</em></p><p><strong>ngDoCheck() : </strong>This Hook is triggered during every <a href="https://blog.angular-university.io/how-does-angular-2-change-detection-really-work/">change detection</a> in the component. it detects and act upon changes.</p><p><strong>ngAfterContentInit() : </strong>This Hook is triggered after Angular projects <a href="https://blog.angular-university.io/angular-ng-content/">external content</a> into the component’s view. Called after the first <em>ngDoCheck and is triggered only once in the entire life-cycle.</em></p><p><strong>ngAfterContentChecked() : </strong>This Hook is triggered each time the content projected into the component is checked by Angular. The Hook is called after the <em>ngAfterContentInit </em>and every subsequent <em>ngDoCheck.</em></p><p><strong>ngAfterViewInit() : </strong>This Hook is triggered when the component’s view has been fully initialized. Only triggered once in the entire life-cycle.</p><p><strong>ngAfterViewChecked() : </strong>This Hook is triggered after the component’s views have been checked by Angular. The Hook is called after the <em>ngAfterViewInit </em>and every subsequent <em>ngAfterContentChecked.</em></p><p><strong>ngOnDestroy() : </strong>This Hook is triggered just before the component is destroyed.</p><h3><strong>Best Practices</strong></h3><p>Now that we have an idea of how an angular component’s life-cycle works, lets look at some of the best practices to adopt when using them.</p><h4>Don’t do heavy/complex logic in the constructor</h4><p>The constructor shouldn’t be loaded with the burden of complex initialization logic, rather use the <em>ngOnInit </em>hook. Simple initialization such as creating new instances of objects should be okay. Also its possible that some input property binding haven’t been processed as at the time the constructor is triggered and thus any variable being initialized with a value might not actually reflect in the template.</p><h4>Don’t use any of the life-cycle hooks as a regular function</h4><p>Take a look at the code below:</p><pre>@Component({<br>    selector: &#39;&#39;,<br>    templateUrl: `&lt;div&gt;<br>                 &lt;button (click)=&#39;ngOnInit()&#39;&gt; FETCH USERS&lt;/button&gt;<br>                  &lt;/div&gt;`      <br>})<br><strong>export class </strong>AppComponent <strong>implements </strong>OnInit{</pre><pre>      constructor() {}</pre><pre>      ngOnInit(): void {<br>       this.fetchUser();<br>      }</pre><pre>      fetchUser() {<br>      /** logic to fetch list of users */<br>      }<br>}</pre><p>From the code above, see how the button in the template is assigned a click event? The function passed is the <em>ngOnInit </em>hook!!. This should be avoided at all cost. Let the hook be the hook and nothing but the hook, lol.</p><p>Better way to do this:</p><pre>@Component({<br>    selector: &#39;&#39;,<br>    templateUrl: `&lt;div&gt;<br>                 &lt;button (click)=&#39;fetchUser()&#39;&gt; FETCH USERS&lt;/button&gt;<br>                  &lt;/div&gt;`      <br>})<br><strong>export class </strong>AppComponent <strong>implements </strong>OnInit{</pre><pre>       constructor() {}</pre><pre>       ngOnInit(): void {<br>         this.fetchUser();<br>       }</pre><pre>       fetchUser() {<br>         /** logic to fetch list of users */<br>       }<br>}</pre><h4>Unsubscribe to Observables in ngOnDestroy hook</h4><p>If you have Observables defined in your component, unsubscribing to them when the component is being destroyed can avoid memory leaks in your application.</p><p>…and lastly, apart from the <strong>constructor</strong>, it’s not compulsory to implement all the life-cycle hooks. Always use them where appropriate/necessary.</p><p>Thanks for your time, until next time…</p><p>Cheers</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=484a41a02bd0" width="1" height="1" alt="">]]></content:encoded>
        </item>
    </channel>
</rss>