<?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: Vladimir Gorelik</title>
    <description>The latest articles on DEV Community by Vladimir Gorelik (@vladimirgorelik8).</description>
    <link>https://dev.to/vladimirgorelik8</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%2F2747156%2F5a6b64d3-9770-42c5-8253-04836648269b.png</url>
      <title>DEV Community: Vladimir Gorelik</title>
      <link>https://dev.to/vladimirgorelik8</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://dev.to/feed/vladimirgorelik8"/>
    <language>en</language>
    <item>
      <title>Mastering the LWC Lifecycle with a Live-Refreshing Contact Table</title>
      <dc:creator>Vladimir Gorelik</dc:creator>
      <pubDate>Sat, 12 Jul 2025 23:02:21 +0000</pubDate>
      <link>https://dev.to/vladimirgorelik8/mastering-the-lwc-lifecycle-with-a-live-refreshing-contact-table-1305</link>
      <guid>https://dev.to/vladimirgorelik8/mastering-the-lwc-lifecycle-with-a-live-refreshing-contact-table-1305</guid>
      <description>&lt;h2&gt;
  
  
  ⚡ Mastering the LWC Lifecycle with a Live-Refreshing Contact Table
&lt;/h2&gt;

&lt;p&gt;In the Salesforce ecosystem, mastering the &lt;strong&gt;Lightning Web Component (LWC) lifecycle&lt;/strong&gt; is more than just knowing when code runs—it's about writing clean, responsive, and performant components that &lt;strong&gt;react to data&lt;/strong&gt;.&lt;/p&gt;

&lt;p&gt;This post is a deep dive into how the lifecycle works, from instantiation to render and re-render—featuring a &lt;strong&gt;real-world use case&lt;/strong&gt;: a Contact Table that fetches data via &lt;code&gt;@wire&lt;/code&gt;, renders conditionally with &lt;code&gt;isLoading&lt;/code&gt;, and refreshes automatically when Contact records are added or changed.&lt;/p&gt;




&lt;h2&gt;
  
  
  🧠 LWC Lifecycle at a Glance
&lt;/h2&gt;

&lt;p&gt;Lifecycle Hooks Used:&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;
&lt;code&gt;constructor()&lt;/code&gt; – component is created&lt;/li&gt;
&lt;li&gt;
&lt;code&gt;connectedCallback()&lt;/code&gt; – added to DOM&lt;/li&gt;
&lt;li&gt;
&lt;code&gt;renderedCallback()&lt;/code&gt; – after DOM rendered&lt;/li&gt;
&lt;li&gt;
&lt;code&gt;@wire&lt;/code&gt; – reactively pulls Contact data&lt;/li&gt;
&lt;li&gt;
&lt;code&gt;refreshApex()&lt;/code&gt; – re-fetches wire data&lt;/li&gt;
&lt;li&gt;
&lt;code&gt;disconnectedCallback()&lt;/code&gt; – cleanup on removal&lt;/li&gt;
&lt;/ol&gt;




&lt;h2&gt;
  
  
  💻 Code Walkthrough: Contact Table Component
&lt;/h2&gt;

&lt;h3&gt;
  
  
  📦 &lt;code&gt;contactTable.js&lt;/code&gt;
&lt;/h3&gt;



&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;import { LightningElement, wire, track } from 'lwc';
import getContacts from '@salesforce/apex/ContactController.getContacts';
import { refreshApex } from '@salesforce/apex';

export default class ContactTable extends LightningElement {
    @track contacts = [];
    @track isLoading = true;
    error;
    wiredResult;
    hasRendered = false;

    columns = [
        { label: 'First Name', fieldName: 'FirstName' },
        { label: 'Last Name', fieldName: 'LastName' },
        { label: 'Phone', fieldName: 'Phone' },
        { label: 'Email', fieldName: 'Email' }
    ];

    @wire(getContacts)
    wiredContacts(result) {
        this.wiredResult = result;
        this.isLoading = true;

        if (result.data) {
            this.contacts = result.data;
            this.error = undefined;
        } else if (result.error) {
            this.contacts = [];
            this.error = result.error;
        }

        this.isLoading = false;
    }

    renderedCallback() {
        if (!this.hasRendered) {
            console.log('Rendered!');
            this.hasRendered = true;
        }
    }

    refreshTable() {
        this.isLoading = true;
        refreshApex(this.wiredResult).finally(() =&amp;gt; {
            this.isLoading = false;
        });
    }
}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h3&gt;
  
  
  🧩 contactTable.html
&lt;/h3&gt;



&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;&amp;lt;template&amp;gt;
    &amp;lt;lightning-card title="Contacts Table"&amp;gt;
        &amp;lt;template if:true={isLoading}&amp;gt;
            &amp;lt;div class="slds-p-around_medium"&amp;gt;
                &amp;lt;lightning-spinner alternative-text="Loading contacts" size="medium"&amp;gt;&amp;lt;/lightning-spinner&amp;gt;
            &amp;lt;/div&amp;gt;
        &amp;lt;/template&amp;gt;

        &amp;lt;template if:true={contacts}&amp;gt;
            &amp;lt;lightning-datatable
                key-field="Id"
                data={contacts}
                columns={columns}
                hide-checkbox-column="true"
                class="slds-p-horizontal_medium"&amp;gt;
            &amp;lt;/lightning-datatable&amp;gt;
        &amp;lt;/template&amp;gt;

        &amp;lt;template if:true={error}&amp;gt;
            &amp;lt;div class="slds-text-color_error slds-p-horizontal_medium"&amp;gt;
                Error loading contact records.
            &amp;lt;/div&amp;gt;
        &amp;lt;/template&amp;gt;
    &amp;lt;/lightning-card&amp;gt;
&amp;lt;/template&amp;gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h3&gt;
  
  
  🔸 Apex Controller: ContactController.cls
&lt;/h3&gt;



&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;public with sharing class ContactController {
    @AuraEnabled(cacheable=true)
    public static List&amp;lt;Contact&amp;gt; getContacts() {
        return [
            SELECT Id, FirstName, LastName, Phone, Email
            FROM Contact
            WHERE Email != null
            ORDER BY LastName ASC
            LIMIT 50
        ];
    }
}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h3&gt;
  
  
  🔄 Refreshing from the Outside
&lt;/h3&gt;

&lt;p&gt;To allow this table to refresh when a contact is created or updated outside the component (e.g. via Flow, another LWC, or UI action), expose the refreshTable() method:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;// parentComponent.js
handleContactChange() {
    this.template.querySelector('c-contact-table')?.refreshTable();
}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h3&gt;
  
  
  💡 Pro Tips
&lt;/h3&gt;

&lt;ul&gt;
&lt;li&gt;Use @track to ensure reactivity on arrays and loading flags.&lt;/li&gt;
&lt;li&gt;Guard renderedCallback() logic using a hasRendered flag to avoid loops.&lt;/li&gt;
&lt;li&gt;Keep your UI responsive with a well-placed isLoading spinner.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;&lt;strong&gt;Follow&lt;/strong&gt; me for more hands-on LWC examples, Flow integrations, and Salesforce development patterns.&lt;/p&gt;

</description>
      <category>lwc</category>
      <category>javascript</category>
      <category>webdev</category>
      <category>apex</category>
    </item>
  </channel>
</rss>
