LWC Concepts
LWC Concepts
LWC Concepts
1. What is LWC?
It is UI Framework that salesforce developers use to create customized
Pages.
It uses Standardized JavaScript Framework, HTML, CSS Without a Third-
Party framework.
These components are reusable ‘building blocks’ that Salesforce Admins can
deploy for a variety of use cases.
2. Lifecycle Hooks
Constructor
ConnectedCallBack
render
RenderedcallBack
DisconnectedCallback
3. Wire Method Syntax
@wire(getRecord, { recordId: '$recordId', fields: ['Account.Name'] })
wiredAccount({ error, data }) {
if (data) {
this.record = data;
this.error = undefined;
} else if (error) {
this.error = error;
this.record = undefined;
}
}
The folder and its files must follow these naming rules.
We can use the on-change attribute to listen for a change to its value. When the
value changes, the handle Change function in the JavaScript file executes.
In Controller
Create multiple HTML files in the component bundle. Import them all and
add a condition in the render () method to return the correct template
depending on the component’s state.
// MultipleTemplates.js
templateOne = true;
Note:
1. Do not use renderedCallback() to change the state of a component, such as loading
values or setting properties. Use getters and setters instead.
2. Don’t update a wire adapter configuration object property in renderedCallback(), as
it can result in an infinite loop.
3. Don’t update a reactive property or field in renderedCallback(), as it can result in an
infinite loop.
1. When it comes to record page Lightning Web Components, there are two types:
A. Screen Quick Actions and
B. Headless Quick Actions.
2. Screen Quick Action
A. These allow users to carry out updates via a modal window, like filling in a form
to create or edit a new record.
3. Headless Quick Action
A. These allow you to carry out an action without the need to display, such as
navigating to another page or dispatching an event.
How do I know which I should use?
Promise.race
1. It is a utility function that allows you to run multiple promises in parallel and
handle the response of the first promise that is resolved or rejected.
2. It takes an array of promises as an argument and returns a new promise that
is resolved or rejected as soon as one of the promises in the array is resolved
or rejected.
10.What is Async and Await?
As you can see, the use of async and await is not limited to just making API calls. In fact,
they're very useful when you want to perform an action that takes a long time. For
example:
Making HTTP requests
Waiting for an asynchronous action to finish
Processing multiple promises in parallel
An async function always returns a Promise, and within an async function, you can
use await to pause the execution until a Promise is resolved.
While both approaches achieve the same result, async/await is often considered
more readable and easier to work with, especially when dealing with complex
asynchronous flows.
The Pub-Sub pattern in LWC was initially used for event communication
between unrelated components on the same Lightning page. LMS allows
communication between components across different pages and
between Visualforce pages and various types of Lightning components,
including LWCs and Aura components.
Wire a property.
Wire a function.
Call a method imperatively.
1. Wire Property with Apex
we can get the data using {property.data} & error using {property.error} in
html file.
Use Case: Business want to create one page to search all account records
base on account name.
Wire Function with Apex
Note: - For Imperative method we don’t need to mark the apex method as cacheabe=true.
Usage: when you need to invoke an Apex method without responsive parameters in
response to a specific event, like a button click event to get record.
1. One of the main differences between the wire and imperative calls is how data is
retrieved and updated. Wire calls provide real-time updates to the data in the
LWC, while imperative calls require the developer to explicitly retrieve and
update the data.
2. Another difference between the two methods is how the data is stored. With wire
calls, the data is stored in a reactive variable that updates automatically
whenever the data on the server-side changes. With imperative calls, the data is
stored in a regular variable and needs to be updated manually.
Automatic data refreshing: Wired apex method calls provide automatic data
refreshing, meaning that any changes made to the data are automatically reflected in
the UI without requiring any additional code.
No need for error handling: Since the wired apex method calls handle errors
automatically, you don’t need to write any additional code for error handling.
Better performance: Wired apex method calls use a reactive programming model
and optimize the data transfer between the server and client, resulting in better
performance.
Cons of using wired apex method call:
Limited data manipulation: Wired apex method calls are read-only and do not
allow for any data manipulation or updates.
Limited control over the data: Wired apex method calls automatically fetch and
cache data, which can result in less control over the data and its behavior.
Limited support for complex queries: Wired apex method calls only support
simple queries and may not be suitable for more complex queries that require
additional logic.
Full control over data: Imperative apex method calls allow full control over data
and its behavior, allowing for DML operations such as updates, inserts, and deletes
as needed.
Support for complex queries: Imperative apex method calls allow for complex
queries and additional logic, making them suitable for more complex use cases.
Better debugging: Since the imperative apex method calls to provide more control
over the data, they also allow for easier debugging of issues.
More error-prone: Imperative apex method calls require more error handling code
since they do not handle errors automatically as wired apex method calls.
Manual data refreshing: Since imperative apex method calls do not provide
automatic data refreshing, you need to write additional code to manually refresh the
data.
Lower performance: Imperative apex method calls require more code and data
transfer between the server and client, resulting in potentially lower performance
compared to wired apex method calls.
Best Practices:
Errors in LWC
1. If you write wire method inside function, leading decorators must be attached to
class which means decorators like wire, track, Api can be directly under class only
not inside any other function.
lightningRecordFormDemo.html
Reference Links
1. https://www.codingninjas.com/codestudio/library/difference-between-var-
let-and-const-in-js
2. https://www.apexhours.com/lightning-message-service-lms-
messagechannel/
3. https://salesforce.stackexchange.com/questions/383706/update-records-in-
lwc-using-apex
4. https://salesforcescool.blogspot.com/2021/11/refresh-datatable-after-edit-
record-in.html
5. https://www.sfdcblogs.com/post/how-to-fetch-inputs-in-lightning-web-
components
6. https://dineshyadav.com/salesforce-lightning-message-service-explained/
7. https://www.crsinfosolutions.com/enhancing-performance-with-dom-
manipulation-in-lightning-web-components/
8. https://www.crsinfosolutions.com/understanding-lifecycle-hooks-in-
lightning-web-components/
9. https://jayakrishnasfdc.wordpress.com/2020/12/06/wire-service-in-
lightning-web-component-lwc/