Before going into details on What are Lightning Web Components? first let us understand What are Web Components?
Web components are a set of web platform APIs that allow you to create new custom, reusable, encapsulated HTML tags to use in web pages and web apps. Custom components and widgets build on the Web Component standards, will work across modern browsers, and can be used with any JavaScript library or framework that works with HTML.
The concept was first introduced in 2011 by Google engineer Alex Russell.
It was in 2013 when Google and the Chrome team developed the first version of this Web Components standard, the V0 version.
It was between 2016 and 2018 when everything changed, Google introduced the Web Components V1 standard, which was adopted by most modern browsers.
Why Web Components?
Let say we select a framework today and tomorrow we want to move to some other framework and share business logic and functionality between different projects however, this might force us, in many occasions to re-code tools and functionalities to adapt them to new projects or services.
This is where Web Components comes into play. Web Components is a tool that allow us to create tools and functionalities that we can use on any framework and on any browser and that are implemented under a standard accepted by the whole community.
Now, as we have the basic idea on what are Web Components let us move on to our topic of discussion What are Lightning Web Components?
As of now, we have built lightning components using the "Lightning Component Framework" using "Aura Component Model" similarly, we can also built lightning components using the "Lightning Web Component Framework".
Lightning Web Component Model is a new programming model built using the HTML Elements and modern JavaScript. It uses core web component standards and leverages custom elements, templates, decorators, modules, shadow DOM, and other new language constructs available in ECMAScript 7 and beyond.
For developing LWC we require "Salesforce Extensions for Visual Studio Code" and for deploying LWC to an org we require "Salesforce CLI".
Why Salesforce has come up with Lightning Web Component Framework?
Earlier salesforce developers used Visual Force pages which is an HTML Tag-based mark-up language to develop web pages and Apex to perform database operations however it was not compatible to build large scale enterprise solutions and complex applications using Visual Force pages.
In 2014, Salesforce launched the "Lightning Component Framework" along with "Aura Component Model" that is used to develop large scale enterprise solutions and complex applications.
In 2014, web standards only offered a limited foundation for the full stack that developers need to build large-scale web applications, i.e. a rendering engine, standard elements, events, and a core language (ECMAScript 5) and the key elements like a component model, templates, modules, and shadow DOM were all missing from the web standards.
Since then there has been a lot of improvement in the Web Components and to keep up the pace and standardization’s of the Web Components according to the huge demand of the web Components across the internet salesforce moved from "Lightning Component Framework" to "Lightning Web Components Framework".
We can built components using any of the model and can place these components on the same lightning page. Both Aura component, LWC make use of Security, LDS and Base lightning components.
LWC vs AURA:
Below are the advantages of using LWC over AURA.
- Improved performance
- Faster loading sites
- Better security, testing, and browser compatibility
- Can be used under AURA.
Important points to note:
1) Aura component and LWC can exist on Same lightning page.
2) Aura component can include LWC
Scratch org is a disposable Salesforce org used for development and testing.
Scratch org can be created for a maximum of 30 days after which scratch org gets deactivated. The default duration for Scratch org is 7 days.
LWC bundle contains an HTML file, a JavaScript file, and a metadata configuration file
and these files are created once we create a Lightning web component.
We can also create a .css file for styling purpose and We can also create SVG file for the purpose of displaying icon.
4) How to render the HTML file conditionally?
If we want to render HTML conditionally we can make use of if:true={propertyName},if:false={propertyName} in nested template tag.
5) How to iterate over an array in HTML file?
We can make use of for:each directive and iterator directive.
for:each directive:
for:each directive is used to render an array. To render an array add the for:each directive to a nested template tag, for:item is used to access the current item, for:index is used to access the current index.
Iterator directive:
If you have the requirement to access the first and the last element in the list use the iterator directive.
A sample HTML file is as shown below.
.html file
<template>
<!--for:each-->
<b>for:each directive</b>
<template for:each={students} for:item="item"
for:index="index">
<p key={item.id}>
{item.Name}
{item.Branch}
</p>
</template>
<!--Iterator
directive-->
<b>Iterator directive</b>
<template iterator:it={students}>
<li
key={it.value.id}>
{it.value.id}
{it.value.Name}
{it.value.Branch}
</li>
<!--To access the
first and the last element use {it.first} and {it.last}-->
</template>
</template>
1) @api
2) @track
3) @wire
#lwc interview questions in salesforce#lwc interview questions salesforce#lwc interview questions and answers
- The HTML file is enclosed within the <template> tags.
- It contains the component HTML.
<template>
<h1> Some Text </h1>
</template>
- The JavaScript file defines the behavior of HTML element present in HTML file.
- JavaScript files in Lightning web components are ES6 modules. By default, everything declared in a module is local—it’s scoped to the module.
- If we want to import a class, function, or variable declared in a module we use the import statement.
- If we want to allow other code to use a class, function, or variable declared in a module we use the export statement.
- The core module in Lightning Web Components is lwc. The import statement imports LightningElement from the lwc module.
export default class MyComponent extends LightningElement {
// Code
}
#lwc interview questions in salesforce#lwc interview questions salesforce#lwc interview questions and answers
constructor()
connectedCallback()
disconnectedCallback()
render()
renderedCallback()
errorCallback(error, stack)
childcompdemo.html
childcompdemo.js
parentcompdemo.html
parentcompdemo.js
lightningapp.app
import { LightningElement, api } from 'lwc';
Parent component can make use of the Public Property.
childcompdemo.html
childcompdemo.js
parentcompdemo.html
parentcompdemo.js
lightningapp.app
childDemoComp.js
parentDemoComp.html
parentDemoComp.js
testApp.app
#lwc interview questions in salesforce#lwc interview questions salesforce#lwc interview questions and answers
To call method of child LWC from parent Aura component we will need to decorate the
child LWC method with @api decorator, In AURA component we need to first assign aura id to child
LWC as highlighted in yellow color. Using component.find() we can get the child LWC and
finally call its method as highlighted in AURA component JS file.
parentAuraComp.cmp
<aura:component>
<div>
I am parent Aura Component.
</div>
<c:childLwcComp aura:id="childLwcCompId"></c:childLwcComp>
<lightning:button label="Call Child LWC Comp" variant="brand" onclick="{!c.callChildMethod}" ></lightning:button>
</aura:component>
parentAuraCompController.js
({
callChildMethod : function(component, event, helper) {
var findChildComp=component.find('childLwcCompId');
findChildComp.lwcCompMethodDescription('Hi I am Aura Component Message');
}
})
childLwcComp.html
<template>
<div>
I am child LWC component.
</div>
</template>
childLwcComp.js
import { LightningElement,api } from 'lwc';
export default class ChildLwcComp extends LightningElement {
@api
lwcCompMethodDescription(messageFromAura){
alert('messageFromAura :'+ messageFromAura);
}
}
auraAppForTesting.app
<aura:application>
<c:parentAuraComp></c:parentAuraComp>
</aura:application>
Output:
By using force:hasRecordId interface in Aura component we can get the id of the current record however in LWC it is very easy to get the id of the current record.
In the component’s JavaScript class, we use @api decorator to create a public recordId property.
If we want to control when the method invocation should occurs (for example, in response to clicking a button) than we call the method imperatively.
When we call a method imperatively, we receive only a single response.
However with @wire, it delegates control to the framework and results in a stream of values being provisioned.
Let us understand with a simple example,
getAccountDataImperatively.html
<template>
<div >
<lightning-input type="text" label="Enter Account Name" value={accName} onchange={handleChange}> </lightning-input>
<lightning-button variant="base" label="Search Account" title="Search Account" onclick={handleChangeMethod} class="slds-m-left_x-small"></lightning-button>
<p>Displaying Account Information</p>
<template if:true={accountRecord}>
<template for:each={accountRecord} for:item="item" for:index="index">
<p key={item.Id}>
Name: {item.Name}
</p>
</template>
</template>
<template if:true={error}>
Some error occured.
</template>
</div>
</template>
import { LightningElement,track } from 'lwc';
import getAccountRecordMethod from '@salesforce/apex/customAccountController.getAccountRecordMethod';
export default class GetAccountDataImperatively extends LightningElement {
@track accountRecord;
@track error;
@track accName;
handleChange(event){
const userInput = event.target.value;
this.accName= userInput;
}
handleChangeMethod() {
getAccountRecordMethod({
accNameParamInApex : this.accName
})
.then(result => {
this.accountRecord = result;
})
.catch(error => {
this.error = error;
});
}
}
<?xml version="1.0" encoding="UTF-8"?>
<LightningComponentBundle xmlns="http://soap.sforce.com/2006/04/metadata">
<apiVersion>50.0</apiVersion>
<isExposed>false</isExposed>
</LightningComponentBundle>
public with sharing class customAccountController {
public customAccountController() {
}
@AuraEnabled(cacheable=true)
public static List<Account> getAccountRecordMethod(String accNameParamInApex) {
String accNameKey = '%'+accNameParamInApex+'%';
List<Account> accList=[SELECT Id, Name, Industry
FROM Account
Where name Like: accNameKey];
return accList;
}
}
<aura:application>
<c:getAccountDataImperatively></c:getAccountDataImperatively>
</aura:application>
Using lightning/uiRecordApi we can perform operations as mentioned below without using Apex.
1) Create a record
2) Get record data
3) Delete a record
4) Get a field value from record
5) Update a record and etc.
37) How to get information related to an object in LWC?
we can make use of getObjectInfo to get information related to an object
such as fields, apiName etc.
38) Is it possible to use design attribute in LWC?
Yes.
For more details visit How to use design attribute in LWC?
#lwc interview questions#lightning web components interview questions#salesforce lwc interview questions#salesforce lightning web components interview questions#lwc salesforce interview questions#interview questions on lwc#interview questions on lwc salesforce#lightning web components interview questions and answers#interview questions on lightning web components
#lwc interview questions in salesforce#lwc interview questions salesforce#lwc interview questions and answers
41) Is it possible to use custom label in LWC?
We use the lightning-record-form to create forms to add, view, or update a record.
It is easier to build forms using lightning-record-form as compared to lightning-record-view-form and lightning-record-edit-form however lightning-record-form is less customizable. To customize the form layout or to provide custom rendering of data use lightning-record-view-form(view a record) and lightning-record-edit-form(add or update).
Thaanks for youl r valuable information.... really helpful
ReplyDeleteVery nice contained. I hope I get a job by reading this soon.
ReplyDeleteThankyou for valuable information
ReplyDelete