Decorators in LWC
September 20, 2023
Decorators in LWC
1- @track
- private property used for same component where it is declared.
- is used to display the value when it changes within the same component.
- contains an object or an array, tells the framework to observe changes to the properties of an object or elements of an array.
- Spring ’20 it is not required
helloWorld.html
<template> <lightning-card title="Hello World" icon-name="custom:custom14"> <div class="slds-m-around_medium"> <p>Hello, {greetingMessage}!</p> <lightning-input label="Name" value={greetingMessage} onchange={changeHandler}></lightning-input> </div> </lightning-card> </template>
helloWorld.js
import { LightningElement } from 'lwc'; export default class HelloWorld extends LightningElement { //@track greetingMessage = 'World';//Before Spring ’20 to need to import track decorator & use it to make a field reactive greetingMessage = 'World'; changeHandler(event) { this.greetingMessage = event.target.value; } }
2- @api
- public property used for cross component.
- is to make a public reactive property.
- generally used when sending data from parent to child component and redirecting from one to another component
** examples of api decorators are in the next posts.
3- @wire
- To call Apex Class.
- Reactive service based of LDS
- @wire service read data either from LDS or Apex
import { getRecord } from 'lightning/uiRecordApi'; import ACCOUNT_NAME_FIELD from '@salesforce/schema/Account.Name';
- is to make one way binding from JavaScript to html, make a private property reactive.
- must set cacheable=true
- Can’t make DML in a @wire method –> Too many DML statements: 1
- Only used for read data
** examples of wire decorators are in the next posts.
4- get
- property always return a value
get Name(){ return 'Hello World'; }
5- set
- sets value to an attribute
set productId(value) { this.productId = value; }
5
2
votes
Article Rating
Subscribe
Login
0 Comments
Oldest
Newest
Most Voted
Inline Feedbacks
View all comments
Latest Post
About Me
Welcome to an Aimer's weblog :)
Hi! Asif Parvez here, I'm from West Bengal's Howrah. I have extensive experience in Apex, Integration (REST API), LWC, ADMIN and working as Senior Salesforce Developer for Dazeworks Technologies(An iLink Digital Company). I am also a content creator and blogger.
I have three certifications(PD-I, PD-II, Salesforce Associate).
Our Visitor
Our Visitor
0
0
0
3
8
2
Users Today : 0
Users Yesterday : 0
Users Last 7 days : 11
Users Last 30 days : 41
Users This Month : 33
Users This Year : 381
Total Users : 382
Views Today :
Views Yesterday :
Views Last 7 days : 43
Views Last 30 days : 79
Views This Month : 70
Views This Year : 715
Total views : 716
Who's Online : 0
Your IP Address : 3.137.181.194
Server Time : 2024-12-23
Powered By WPS Visitor Counter