<?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"><channel><title><![CDATA[Untitled Publication]]></title><description><![CDATA[Untitled Publication]]></description><link>https://shreyventure.hashnode.dev</link><image><url>https://cdn.hashnode.com/res/hashnode/image/upload/v1678032826973/mYx2zTLL2.png</url><title>Untitled Publication</title><link>https://shreyventure.hashnode.dev</link></image><generator>RSS for Node</generator><lastBuildDate>Tue, 20 Oct 2026 13:47:52 GMT</lastBuildDate><atom:link href="https://shreyventure.hashnode.dev/rss.xml" rel="self" type="application/rss+xml"/><language><![CDATA[en]]></language><ttl>60</ttl><item><title><![CDATA[Parent-to-Child and Child-to-Parent communication in LWC]]></title><description><![CDATA[The article discusses Lightning Web Components (LWC) communication and how they can be used to build interactive web applications.]]></description><link>https://shreyventure.hashnode.dev/parent-to-child-and-child-to-parent-communication-in-lwc</link><guid isPermaLink="true">https://shreyventure.hashnode.dev/parent-to-child-and-child-to-parent-communication-in-lwc</guid><category><![CDATA[JavaScript]]></category><category><![CDATA[javascript framework]]></category><category><![CDATA[Lightning web components]]></category><category><![CDATA[Salesforce]]></category><category><![CDATA[lwc]]></category><dc:creator><![CDATA[Shreyas]]></dc:creator><pubDate>Sat, 04 Mar 2023 13:09:25 GMT</pubDate><enclosure url="https://cdn.hashnode.com/res/hashnode/image/upload/v1678034980800/9b0ac84a-7323-4f58-b813-522aca163d27.jpeg" length="0" type="image/jpeg"/><content:encoded><![CDATA[<p>If you've been struggling to wrap your head around component communication in LWC, welcome to the club. When I started developing lightning web components, I had a tough time figuring out how to pass parameters from one component to another. And let's face it, things can get even trickier if you're new to web development or haven't worked with frameworks like React.js or Angular.js.</p>
<p>In this article, I endeavor to delineate the complexities of component communication, offering a simplified and approachable means of understanding this fundamental concept.</p>
<hr />
<h3 id="heading-analogical-explanation">Analogical explanation</h3>
<p>Before penning this article, I pondered the optimal means of articulating this topic to those without a background in web development. Ultimately, I concluded that an analogy would best serve to facilitate comprehension.</p>
<p>Let's examine a scenario to demonstrate parent-child and child-parent communication in LWCs. Imagine a father and his child playing catch in their backyard. The father <em>throws</em> the ball to the child, who <em>deftly</em> catches it. However, when the child attempts to return the ball by throwing it back to the father, it falls short, bringing the game to an abrupt end due to the child's limited strength. In a bid to overcome this situation, the ingenious child devises a solution by utilizing a small, <em>remote-controlled pickup truck</em> to carry the ball back to the father, ultimately keeping this, (now so-called) game of catch going.</p>
<p>At this point, you may be questioning why the father and child didn't simply reduce the distance between them. Well hold on you smart brainiacs, let me get my point straight. In this analogy, the father represents the parent component, while the child represents the child component, both attempting to pass the ball (i.e., parameter) to each other. The sole variation here lies in the method by which they pass the ball.</p>
<h3 id="heading-parent-to-child-communication">Parent-to-child communication</h3>
<p>In LWC, a child component is nested within the parent component, meaning that the parent component is always wrapped around the child component. This arrangement allows the parent component to transmit data to the child component directly (analogically, throw the ball) via HTML. On the other hand, LWC provides a means to retrieve data sent by the parent component in the child component through a popular annotation(child's adept skills) known as <code>@api</code>.</p>
<p>Consider the code provided below.</p>
<pre><code class="lang-javascript"><span class="hljs-comment">// c/parent</span>
<span class="hljs-keyword">import</span> { LightningElement } <span class="hljs-keyword">from</span> <span class="hljs-string">'lwc'</span>;

<span class="hljs-keyword">export</span> <span class="hljs-keyword">default</span> <span class="hljs-class"><span class="hljs-keyword">class</span> <span class="hljs-title">Parent</span> <span class="hljs-keyword">extends</span> <span class="hljs-title">LightningElement</span> </span>{
    data = <span class="hljs-string">"Ball"</span>
}
</code></pre>
<pre><code class="lang-xml"><span class="hljs-comment">&lt;!-- c/parent --&gt;</span>
<span class="hljs-tag">&lt;<span class="hljs-name">template</span>&gt;</span>
    <span class="hljs-tag">&lt;<span class="hljs-name">lightning-card</span> <span class="hljs-attr">title</span>=<span class="hljs-string">"Parent"</span> <span class="hljs-attr">class</span>=<span class="hljs-string">"slds-var-p-around_small"</span>&gt;</span>
        <span class="hljs-tag">&lt;<span class="hljs-name">c-child</span> <span class="hljs-attr">parent-data</span>=<span class="hljs-string">{data}</span>&gt;</span><span class="hljs-tag">&lt;/<span class="hljs-name">c-child</span>&gt;</span>
    <span class="hljs-tag">&lt;/<span class="hljs-name">lightning-card</span>&gt;</span>
<span class="hljs-tag">&lt;/<span class="hljs-name">template</span>&gt;</span>
</code></pre>
<p>The above code represents the JavaScript and HTML of the parent component. The parent component includes a property called <code>data</code>, which is transmitted to the child component via HTML.</p>
<p>Let's take a look at the child component.</p>
<pre><code class="lang-javascript"><span class="hljs-comment">// c/child</span>
<span class="hljs-keyword">import</span> { LightningElement, api } <span class="hljs-keyword">from</span> <span class="hljs-string">'lwc'</span>;

<span class="hljs-keyword">export</span> <span class="hljs-keyword">default</span> <span class="hljs-class"><span class="hljs-keyword">class</span> <span class="hljs-title">Child</span> <span class="hljs-keyword">extends</span> <span class="hljs-title">LightningElement</span> </span>{
    @api parentData;
}
</code></pre>
<pre><code class="lang-xml"><span class="hljs-comment">&lt;!-- c/child --&gt;</span>
<span class="hljs-tag">&lt;<span class="hljs-name">template</span>&gt;</span>
    <span class="hljs-tag">&lt;<span class="hljs-name">lightning-card</span> <span class="hljs-attr">title</span>=<span class="hljs-string">"Child"</span> <span class="hljs-attr">class</span>=<span class="hljs-string">"my-card slds-tabs_card"</span>&gt;</span>
        <span class="hljs-tag">&lt;<span class="hljs-name">p</span>&gt;</span>Caught the {parentData}<span class="hljs-tag">&lt;/<span class="hljs-name">p</span>&gt;</span>
    <span class="hljs-tag">&lt;/<span class="hljs-name">lightning-card</span>&gt;</span>
<span class="hljs-tag">&lt;/<span class="hljs-name">template</span>&gt;</span>
</code></pre>
<p>The <code>parentData</code> property of the child component has been annotated by <code>@api</code> decorator here. This decorator, in technical terms, makes a property <em>public</em> in nature, ie. its value can be populated by any component that encloses it (parent component). While you are at it, you may also compare the HTML code of the parent component with the JavaScript code of the child component. A notable difference is that the <code>parentData</code> property in the child component, which is written in camel case, is been addressed by the parent using kebab case(<code>parent-data</code>). This conversion of cases is common in LWC and you can read more about it <a target="_blank" href="https://developer.salesforce.com/docs/component-library/documentation/en/lwc/js_props_names">here</a>.</p>
<p>This is how the code output looks so far.</p>
<p><img src="https://cdn.hashnode.com/res/hashnode/image/upload/v1677926870872/43ba5034-3dfe-4a00-afb8-d9f1f4839b65.png" alt class="image--center mx-auto" /></p>
<h3 id="heading-child-to-parent-communication">Child-to-parent communication</h3>
<p>Well, now that the child has the ball, it wants to send it back to the father. The parent component could send the data to the child through HTML, but how do we send data from child to parent, when the parent isn't present in the child's HTML? To put it simply, the child component doesn't know who its parent is, so it cannot directly send data back to the parent. In order to send data back to the parent, the child component needs to take the help of its <em>remote-controlled truck</em>, an event. This event is dispatched by the child component and then handled by the parent component using an event handler.</p>
<p>To demonstrate this, let's add a button to the child component that when clicked sends some content to the parent component.</p>
<pre><code class="lang-xml"><span class="hljs-comment">&lt;!-- c/child --&gt;</span>
<span class="hljs-tag">&lt;<span class="hljs-name">template</span>&gt;</span>
  <span class="hljs-tag">&lt;<span class="hljs-name">lightning-card</span> <span class="hljs-attr">title</span>=<span class="hljs-string">"Child"</span> <span class="hljs-attr">class</span>=<span class="hljs-string">"my-card slds-tabs_card"</span>&gt;</span>
    <span class="hljs-tag">&lt;<span class="hljs-name">p</span>&gt;</span>Caught the {parentData}<span class="hljs-tag">&lt;/<span class="hljs-name">p</span>&gt;</span>
    <span class="hljs-comment">&lt;!-- New Button --&gt;</span>
    <span class="hljs-tag">&lt;<span class="hljs-name">lightning-button</span>
      <span class="hljs-attr">label</span>=<span class="hljs-string">"Send data back using the truck🛻"</span>
      <span class="hljs-attr">onclick</span>=<span class="hljs-string">{sendBackTheBall}</span>
    &gt;</span><span class="hljs-tag">&lt;/<span class="hljs-name">lightning-button</span>&gt;</span>
    <span class="hljs-comment">&lt;!-- New Button --&gt;</span>
  <span class="hljs-tag">&lt;/<span class="hljs-name">lightning-card</span>&gt;</span>
<span class="hljs-tag">&lt;/<span class="hljs-name">template</span>&gt;</span>
</code></pre>
<p>Now that we have a button that on clicked, calls the function <code>sendBackTheBall</code>, let's add that function to the JavaScript file of the child component. this function primarily dispatches an event to transmit data to the parent component.</p>
<pre><code class="lang-javascript"><span class="hljs-comment">// c/child</span>
<span class="hljs-keyword">import</span> { LightningElement, api } <span class="hljs-keyword">from</span> <span class="hljs-string">"lwc"</span>;

<span class="hljs-keyword">export</span> <span class="hljs-keyword">default</span> <span class="hljs-class"><span class="hljs-keyword">class</span> <span class="hljs-title">Child</span> <span class="hljs-keyword">extends</span> <span class="hljs-title">LightningElement</span> </span>{
  @api parentData;

  sendBackTheBall() {
    <span class="hljs-built_in">this</span>.dispatchEvent(
      <span class="hljs-keyword">new</span> CustomEvent(<span class="hljs-string">"sendbacktheball"</span>, {
        <span class="hljs-attr">detail</span>: <span class="hljs-string">"ball"</span>
      })
    );
  }
}
</code></pre>
<p>Here, the <code>sendBackTheBall</code> function creates a new custom event using the <code>CustomEvent</code> class. The event is given the name <code>sendbacktheball</code> (all lowercase characters) and some data is passed in the <code>detail</code> property of the custom event. Finally, the event is dispatched using the <code>dispatchEvent</code> function, which is bound to the current context.</p>
<p>As far as the parent component goes, it needs to listen to the event emitted by the child, which can be done directly through the parent component's HTML code.</p>
<pre><code class="lang-javascript">&lt;!-- c/parent --&gt;
<span class="xml"><span class="hljs-tag">&lt;<span class="hljs-name">template</span>&gt;</span>
  <span class="hljs-tag">&lt;<span class="hljs-name">lightning-card</span> <span class="hljs-attr">title</span>=<span class="hljs-string">"Parent"</span>&gt;</span>
    <span class="hljs-tag">&lt;<span class="hljs-name">p</span> <span class="hljs-attr">class</span>=<span class="hljs-string">"slds-var-m-horizontal_medium"</span>&gt;</span>Recieved the {childData}<span class="hljs-tag">&lt;/<span class="hljs-name">p</span>&gt;</span>
    <span class="hljs-tag">&lt;<span class="hljs-name">c-child</span> <span class="hljs-attr">parent-data</span>=<span class="hljs-string">{data}</span> <span class="hljs-attr">onsendbacktheball</span>=<span class="hljs-string">{handleChildData}</span>&gt;</span><span class="hljs-tag">&lt;/<span class="hljs-name">c-child</span>&gt;</span>
  <span class="hljs-tag">&lt;/<span class="hljs-name">lightning-card</span>&gt;</span>
<span class="hljs-tag">&lt;/<span class="hljs-name">template</span>&gt;</span></span>
</code></pre>
<p>In the above code of the parent component, you can observe that a new attribute <code>onsendbacktheball</code> has been added to the <code>&lt;c-child&gt;</code> element in the parent's HTML, followed by an equals sign and a function name <code>handleChildData</code>. By adding an attribute with the event name prefixed with 'on', the parent can listen to a child component's event. Moreover, the HTML also includes a new <code>&lt;p&gt;</code> tag above the child element, which displays a new property called <code>childData</code>.</p>
<pre><code class="lang-javascript"><span class="hljs-comment">// c/parent</span>
<span class="hljs-keyword">import</span> { LightningElement } <span class="hljs-keyword">from</span> <span class="hljs-string">'lwc'</span>;

<span class="hljs-keyword">export</span> <span class="hljs-keyword">default</span> <span class="hljs-class"><span class="hljs-keyword">class</span> <span class="hljs-title">Parent</span> <span class="hljs-keyword">extends</span> <span class="hljs-title">LightningElement</span> </span>{
    data = <span class="hljs-string">"Ball"</span>
    childData;

    handleChildData(event) {
        <span class="hljs-built_in">this</span>.childData = event.detail
    }
}
</code></pre>
<p>The aforementioned JavaScript code pertaining to the parent component features the function <code>handleChildData</code>, which serves to assign the value of the <code>event.detail</code> value (which is sent by the child component) to the <code>childData</code> property.</p>
<p>This is the final result of our parent-child component in action. Pretty impressive isn't it?</p>
<p><img src="https://cdn.hashnode.com/res/hashnode/image/upload/v1677930866571/1541cbaf-0a5f-49f6-8e51-44ef877186eb.gif" alt class="image--center mx-auto" /></p>
<p>Phew!😥 Congratulations on making it to the end of the article! It was a lot of information to digest, so well done for sticking with it.👏 If you're feeling inspired, I encourage you to start building your own lightning web components. Practice is the best way to solidify your understanding of the concepts covered in this article. While my analogy of playing catch with a remote-controlled pickup truck might not be the most practical solution, it does serve as a useful reminder that sometimes the best solutions come from thinking outside the box.</p>
<p>I also wanted to mention that this was my first article🥳, so please forgive me if there were any mistakes or areas for improvement. I would greatly appreciate any feedback you may have on how I can improve my writing in the future. Thank you for reading!</p>
]]></content:encoded></item></channel></rss>