The best way to compare the Aura Component and Lightning Web Components programming models is to look at code for similar components written in the two models. The goal of this post is to help you to leverage your Aura components skills to accelerate learning about Lightning web components. You learn how the two types of components work well together.
Prerequisites
Before you dive into this post, complete the Lightning Web Components Basics module, which gives you a great introduction to the new programming model.
I am assuming that you’re familiar with the Aura Components programming model, and we won’t explain its features, except in comparison to the Lightning Web Components programming model. If you’re not familiar with Aura components, start with the Aura Components Basics module.
Aura Initializers Become Lifecycle Hooks
Replace an init event handler in an Aura component with the standard JavaScript connectedCallback() method in a Lightning web component. The connectedCallback() lifecycle hook fires when a component is inserted into the DOM. Lifecycle hooks are callback methods that let you run code at each stage of a component’s lifecycle.We use the init event in an Aura component to initialize a component after component construction but before rendering.
Here’s an init event handler in the PropertyCarousel Aura component.
<aura:handler name="init" value="{!this}" action="{!c.onInit}"/>
The onInit function in the Aura component’s controller performs any necessary initialization.In a Lightning web component, use connectedCallback() instead in the component’s JavaScript file. Here’s an example inpropertySummary.js.
export default class PropertySummary extends LightningElement {
...
connectedCallback() {
// initialize component
}
}
Migrate Base Components
This Aura component uses the lightning:formattedNumber base component.
<aura:component>
<lightning:formattedNumber value="5000" style="currency"
currencyCode="USD"/>
</aura:component>
To migrate this markup to a Lightning web component:
- Change the colon that separates the namespace (lightning) and component name (formattedNumber) to a dash.
- Change the camel-case component name (formattedNumber) to a dash-separated name (formatted-number).
- Change the camel-case attribute name (currencyCode) to a dash-separated name (currency-code).
Here’s the equivalent Lightning web component.
<template>
<lightning-formatted-number value="5000" style="currency"
currency-code="USD" >
</lightning-formatted-number>
</template>
Aura CSS Becomes Standard CSS
Lightning web components use standard CSS syntax. Remove the proprietary THIS class that Aura components use.
Here’s a portion of the CSS file for the PropertyTile Aura component.
.THIS .lower-third {
position: absolute;
bottom: 0;
left: 0;
right: 0;
color: #FFF;
background-color: rgba(0, 0, 0, .3);
padding: 6px 8px;
}
Similar CSS for the propertyTile Lightning web component uses standard CSS instead. The THIS keyword is specific to Aura and isn’t used in Lightning web components.
.lower-third {
position: absolute;
bottom: 0;
left: 0;
right: 0;
color: #FFF;
background-color: rgba(0, 0, 0, .3);
padding: 6px 8px;
}
Call Apex Methods
Here’s a portion of the js file for the calling apex method “getContacts” in Aura component.
var action = component.get("c.getContacts");
action.setCallback(this, function(data) {
component.set("v.Contacts", data.getReturnValue());
});
$A.enqueueAction(action);
The JavaScript function calls the getContacts
method of the Apex controller. It then populates the attribute named Contacts
with the results.
Here’s a portion of the js file for the calling apex method “getContacts” in lightning web component.
import getContacts from '@salesforce/apex/ContactController.getContacts';
export default class ApexImperativeMethod extends LightningElement {
@track contacts;
@track error;
handleLoad() {
getContacts()
.then(result => {
this.contacts = result;
this.error = undefined;
})
.catch(error => {
this.error = error;
this.contacts = undefined;
});
}
}
Also don’t forget to review awesome Salesforce Lightning Web Component Cheat Sheet (Unofficial) prepared by @Santanu Boral
References:
Lightning Web Components for Aura Developers
Working with Aura and Lightning Web Components: Interoperability and Migration