<?xml version="1.0" encoding="UTF-8"?><rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
	>

<channel>
	<title>LWC Archives - Michael Soriano</title>
	<atom:link href="https://michaelsoriano.com/tag/lwc/feed/" rel="self" type="application/rss+xml" />
	<link>https://michaelsoriano.com/tag/lwc/</link>
	<description>I turn code into captivating user experiences for the web</description>
	<lastBuildDate>Sat, 23 Apr 2022 16:51:05 +0000</lastBuildDate>
	<language>en-US</language>
	<sy:updatePeriod>
	hourly	</sy:updatePeriod>
	<sy:updateFrequency>
	1	</sy:updateFrequency>
	<generator>https://wordpress.org/?v=6.7.4</generator>
	<item>
		<title>Salesforce&#8217;s LWC (Lightning Web Components) &#8211; My First Thoughts</title>
		<link>https://michaelsoriano.com/salesforces-lwc-lightning-web-components/</link>
					<comments>https://michaelsoriano.com/salesforces-lwc-lightning-web-components/#comments</comments>
		
		<dc:creator><![CDATA[Michael Soriano]]></dc:creator>
		<pubDate>Mon, 28 Sep 2020 04:41:08 +0000</pubDate>
				<category><![CDATA[Salesforce]]></category>
		<category><![CDATA[LWC]]></category>
		<guid isPermaLink="false">http://michaelsoriano.com/?p=7239</guid>

					<description><![CDATA[<p>Oh no, another JavaScript framework to learn. At first, it sounds bad. But its a good thing actually. Because being stagnant in coding is bad. Always learning new things is the way to go. Besides, newer frameworks (especially in JavaScript) show what older frameworks could&#8217;ve done better. This is the case with Lightning Web Components [&#8230;]</p>
<p>The post <a href="https://michaelsoriano.com/salesforces-lwc-lightning-web-components/">Salesforce&#8217;s LWC (Lightning Web Components) &#8211; My First Thoughts</a> appeared first on <a href="https://michaelsoriano.com">Michael Soriano</a>.</p>
]]></description>
										<content:encoded><![CDATA[
<p>Oh no, another JavaScript framework to learn. At first, it sounds bad. But its a good thing actually. Because being stagnant in coding is bad. Always learning new things is the way to go. </p>



<p>Besides, newer frameworks  (<em>especially in JavaScript</em>) show what older frameworks could&#8217;ve done better. This is the case with Lightning Web Components (LWC). </p>



<figure class="wp-block-image size-large"><img fetchpriority="high" decoding="async" width="1366" height="1015" src="https://michaelsoriano.com/wp-content/uploads/2020/09/undraw_code_review_l1q9.png" alt="Man with Code Background " class="wp-image-7263" srcset="https://michaelsoriano.com/wp-content/uploads/2020/09/undraw_code_review_l1q9.png 1366w, https://michaelsoriano.com/wp-content/uploads/2020/09/undraw_code_review_l1q9-300x223.png 300w, https://michaelsoriano.com/wp-content/uploads/2020/09/undraw_code_review_l1q9-1024x761.png 1024w, https://michaelsoriano.com/wp-content/uploads/2020/09/undraw_code_review_l1q9-768x571.png 768w" sizes="(max-width: 1366px) 100vw, 1366px" /></figure>



<p>LWC is Salesforce&#8217;s newest iteration of Lightning called <a href="https://michaelsoriano.com/tag/aura/" data-type="URL" data-id="https://michaelsoriano.com/tag/aura/">Aura</a>. Actually, iteration is an understatement. It&#8217;s more like an overhaul. See, Aura is a pretty old framework &#8211; I would say at least a decade old. And its the way I learned how to code Salesforce apps. As newer JS frameworks come up, Salesforce decides to up their game. </p>



<p>Enter Lightning Web Components, <strong>LWC </strong>for short.</p>



<p>I started using LWC for a couple of small components, and I&#8217;m liking it a lot. LWC uses web standards and less boilerplate. Just JavaScript, CSS and HTML. </p>



<h3 class="wp-block-heading">Import / Export</h3>



<p>This is probably the biggest change. You can now write modules &#8211; just how JS intended it to be. </p>



<pre class="wp-block-code"><code lang="javascript" class="language-javascript">//importing modules into your component:
import { foo , bar } from "/path/to/lwc-component";
//exporting from your component
export { func1, func2}
//or you can export the entire file
export default class MyClass { .... </code></pre>



<p>No more clunky adding components in the HTML so you can access the helpers. Or extending components, or including static resources &#8211; like how its done with Aura. </p>



<p>This is a big change and I&#8217;m really enjoying it. </p>



<h3 class="wp-block-heading">Component Communication</h3>



<p>I happen to like the two way data-binding that existed in Aura. But the new way of passing data between components is not that bad. After a few uses, it&#8217;s actually quite neat. </p>



<p><strong><span style="text-decoration: underline;">Parent to child</span></strong> is straightforward. Just like other frameworks &#8211; using &#8220;props&#8221; or component attributes.   </p>



<pre class="wp-block-code"><code lang="javascript" class="language-javascript">//parent html:
&lt;c-child some-data={someData}&gt;&lt;/c-child&gt;
//access it inside the child
export default class Child extends LightningElement {
   @api someData
...
// marking a property with @api makes it public - and writeable
// now each time parent changes someData - child changes as well</code></pre>



<p>Keep in mind binding is now uni-directional (from Top to Bottom). When we need to pass data upwards, we pass events. </p>



<p>Oh, to fire a child component&#8217;s method from the parent, you can simply do something like :</p>



<pre class="wp-block-code"><code lang="javascript" class="language-javascript">this.template.querySelector("c-child").someEventInChild()</code></pre>



<p>Make sure you make the method public by putting the &#8220;@api&#8221; on it.</p>



<p><strong><span style="text-decoration: underline;">Child to Parent</span></strong> is a little bit different than the example above. We still use attributes, but the attributes are actually custom events that fire.  </p>



<pre class="wp-block-code"><code lang="javascript" class="language-javascript">//inside child JS
sendEmail () {
        const params = {somedata : "hello there..."};
        const evt = new CustomEvent("sendemail", {detail: params });
        this.dispatchEvent(evt);
    }
//note the custom event "sendemail"</code></pre>



<p>The parent component will use the custom event that is dispatched from the child, but will only find it by prefixing an &#8220;on&#8221; with the event name: </p>



<pre class="wp-block-code"><code lang="javascript" class="language-javascript">//inside parent HTML:
&lt;c-child onsendemail={sendSingleEmail}
&gt;&lt;/c-child&gt;</code></pre>



<p>Now you have access to the parameters sent (inside the &#8220;detail&#8221; property) of the parent function &#8220;<em>sendSingleEmail</em>&#8220;:</p>



<pre class="wp-block-code"><code lang="javascript" class="language-javascript">sendSingleEmail(event){
      console.log(JSON.stringify(event.detail) // here is our stuff..
}</code></pre>



<p>Now, what about if the components are siblings? It would be cumbersome to use the techniques above just correct? And it doesn&#8217;t sound right if we have to reorder our components &#8211; just to avoid component sibling communication. </p>



<p><strong><span style="text-decoration: underline;">Sibling Components</span></strong> &#8211; use <em>Lightning Message Service.</em> </p>



<p>This is a component itself used for &#8220;publishing&#8221; and &#8220;subscribing&#8221; to data. This will solve our issue with sibling communication. I will not go through the process of how to use this service, you can read about that <a href="https://developer.salesforce.com/docs/component-library/bundle/lightning-message-service/documentation">here</a>: </p>



<p>Then in one component (where you want to receive data), you should be able to do something like below: </p>



<pre class="wp-block-code"><code lang="javascript" class="language-javascript">subscribe(this.messageContext, YourChannelName, (message) =&gt; {
    this.items = message.items;
});</code></pre>



<p>That callback method in the end with the &#8220;message&#8221; parameter &#8211; can be used for all of your data manipulation in your current component. </p>



<p>Then in the other component, where the data is coming from, you do a &#8220;<em>publish</em>&#8220;.</p>



<pre class="wp-block-code"><code lang="javascript" class="language-javascript">publish(this.messageContext, YourChannelName, {items : this.items}); </code></pre>



<p>Pretty simple right? Of course there is quite a bit that I&#8217;m leaving out &#8211; but that&#8217;s up to you to dig up 😁</p>



<p>Overall, component communication is definitely harder than Aura. It does however remind me a little bit of <a href="https://michaelsoriano.com/redux-a-quick-and-high-level-overview/" data-type="URL" data-id="https://michaelsoriano.com/redux-a-quick-and-high-level-overview/">Redux</a>. </p>



<h3 class="wp-block-heading" id="apexmethodpromise">Apex Methods are JavaScript Promises</h3>



<p>This one is Salesforce specific &#8211; but is still quite nice. Now you can use the &#8220;import&#8221; statement, for an aura enabled method in your Apex class. An aura enabled method is one that you can call from your front end component. </p>



<p>And if you&#8217;re sill lost &#8211; Apex is Salesforce&#8217;s server side language, while JavaScript Promises are well.. here&#8217;s an <a href="https://michaelsoriano.com/working-with-jquerys-ajax-promises-and-deferred-objects/">old article</a> of what they are.</p>



<p>So you can do something like: </p>



<pre class="wp-block-code"><code lang="javascript" class="language-javascript">import getRecord from '@salesforce/apex/YourClass.getRecord';
//then you can use it like below:
let params = {recordid : id};
getRecord(params).then((result)=&gt;{
    //process result...
}).catch((er)=&gt;{
    //process error
})</code></pre>



<p>The imported method from Apex becomes a JS promise &#8211; where you can handle it like regular JavaScript. You no longer have to muck with &#8220;<em><a href="https://michaelsoriano.com/call-apex-from-lightning-components/" data-type="URL" data-id="https://michaelsoriano.com/call-apex-from-lightning-components/">$A.enqueueAction(action)</a></em>&#8221; like before.</p>



<p>The only thing I don&#8217;t like is when you want to use multiple methods &#8211; you have to import them one at a time. I&#8217;m not sure if you can import all aura methods of a class. Maybe in future releases.</p>



<h3 class="wp-block-heading">Templates</h3>



<p>Not a Templating language like Liquid or Handlebars, just the template tag in HTML. Yes &#8211; LWC is composed of all template tags for logic in HTML. </p>



<p>I wouldn&#8217;t say it&#8217;s the most elegant code, but I&#8217;m getting used to it. Seeing something like: </p>



<pre class="wp-block-code"><code lang="markup" class="language-markup"> &lt;template if:true={hasError}&gt;
     &lt;h3&gt;There's an error&lt;/h3&gt;
 &lt;/template&gt;
&lt;template if:false={hasError}&gt;
     //continue application code
&lt;/template&gt; </code></pre>



<p>It just feels a little weird. But this is not Salesfoce&#8217;s fault. This is web standards and that&#8217;s the way it has to be. </p>



<p>And that&#8217;s about it so far. Like I said, I am enjoying using LWC and learning it at the same time. </p>



<p>I do wish that Salesforce provide better docs. Most of the time I search for something &#8211; and it leads to a Github repo. And we&#8217;re just supposed to dig through the code to find an answer. </p>



<p>Its your turn. </p>



<p>Have you tried LWC? What do you think? Leave your comments below. </p>
<p>The post <a href="https://michaelsoriano.com/salesforces-lwc-lightning-web-components/">Salesforce&#8217;s LWC (Lightning Web Components) &#8211; My First Thoughts</a> appeared first on <a href="https://michaelsoriano.com">Michael Soriano</a>.</p>
]]></content:encoded>
					
					<wfw:commentRss>https://michaelsoriano.com/salesforces-lwc-lightning-web-components/feed/</wfw:commentRss>
			<slash:comments>1</slash:comments>
		
		
			</item>
	</channel>
</rss>
