Skip to content
  • linkedIn
  • twitter
  • About Us
  • News letter
  • Privacy Policy
  • Contact Us
sfdcgenius

sfdcGenius

Learn Practice and Execute

salesforce
  • Home
  • Blog
  • Apex
  • Toggle search form
What are Decorators in Lightning Web Component (LWC)?

What are Decorators in Lightning Web Component (LWC)?

Posted on February 8, 2023February 9, 2023 By sfdcGenius No Comments on What are Decorators in Lightning Web Component (LWC)?

Table of Contents

  • What are Decorators in Lightning Web Component (LWC)?
  • Type of Decorators in Lightning Web Component
    • 1. @api :
    • 2. @track :
    • 3. @wire :
  • Use of @api
    • parent.html
    • parent.js
    • parent.js-meta.xml
    • child.html
    • child.js
    • child.js-meta.xml
  • output:

What are Decorators in Lightning Web Component (LWC)?

The Lightning Web Components programming model has three decorators that add functionality to a property or function. The ability to create decorators is part of ECMAScript, but these three decorators are unique to Lightning Web Components.
Decorators are often used in JavaScript to modify the behavior of a property or function.
To use a decorator, import it from the lwc module and place it before the property or function.

import { LightningElement, api } from 'lwc';
export default class MyComponent extends LightningElement{
    @api message;
}

You can import multiple decorators, but a single property or function can have only one decorator.
For example, a property can’t have @api and @wire decorators.

Type of Decorators in Lightning Web Component

There are three types of Decorators in Lightning web components.

  1. Api
  2. Track
  3. Wire

1. @api :

Marks a field as public. Public properties define the API for a component. A user that uses the component in its HTML markup can access the component’s public properties.
All public properties are reactive, which means that the framework observes the property for changes. When the property’s value changes, the framework reacts and rerenders the component.

In a Lightning web component, only fields that a you decorates with @api are publicly available as object properties.

2. @track :

Tells the framework to observe changes to the properties of an object or to the elements of an array. If a change occurs, the framework rerenders the component.
All fields are reactive. If the value of a field changes and the field is used in a template—or in the getter of a property used in a template—the framework rerenders the component.
You don’t need to decorate the field with @track. Use @track only if a field contains an object or an array and if you want the framework to observe changes to the properties of the object or to the elements of the array. If you want to change the value of the whole property, you don’t need to use @track.

1.Prior to Spring ’20, you had to use @track to mark fields (also known as private properties) as reactive.

2.You’re no longer required to do that. Use @track only to tell the framework to observe changes to the properties of an object or to the elements of an array.

3. @wire :

Gives you an easy way to get and bind data from a Salesforce org.
To read Salesforce data, Lightning web components use a reactive wire service. When the wire service provisions data, the component re-renders. Components use @wire in their JavaScript class to specify a wire adaptor or an Apex method.

Here’s an example of using the @api decorator to render a value from one component (parent) to another component (child). The file structure looks like this:

Decorators in Lightning Web Component (LWC)
lwc structure

do you know what is Modal/Popup in Lightning Web Component(LWC)? Modal/Popup in LWC

Use of @api

parent.html

<!-- parent.html -->
<template>
    <div class="lds-box slds-theme_shade slds-theme_alert-texture" style="padding-left: 15px;">
        <c-child child={child}></c-child>
    </div>
</template>

parent.js

import { LightningElement } from 'lwc';
export default class Parent extends LightningElement {
    child = 'From Parent to child.';
}

parent.js-meta.xml

<?xml version="1.0" encoding="UTF-8"?>
<LightningComponentBundle xmlns="http://soap.sforce.com/2006/04/metadata">
    <apiVersion>56.0</apiVersion>
    <isExposed>true</isExposed>
    <targets>
        <target>lightning__HomePage</target>
      </targets>
</LightningComponentBundle>

child.html

<!-- child.html -->
<template>
    <div class="lds-box slds-theme_shade slds-theme_alert-texture" style="height: 40px;width: 1000px;">
         <p>{child}</p>
    </div>
</template>

child.js

import { LightningElement,api } from 'lwc';
export default class Child extends LightningElement {
    @api child;
}

child.js-meta.xml

<?xml version="1.0" encoding="UTF-8"?>
<LightningComponentBundle xmlns="http://soap.sforce.com/2006/04/metadata">
    <apiVersion>56.0</apiVersion>
    <isExposed>true</isExposed>
    <targets>
        <target>lightning__HomePage</target>
      </targets>
</LightningComponentBundle>

output:

Decorators in Lightning Web Component (LWC)
lwc component

For any Queries/doubts comment below and for quick responses connect on LinkedIn and Twitter.

Related Post

Lightning Web Component, Ligthing, LWC, Recent Post Tags:@api, @track, @wire, api, decorators, lightning web component, lwc, Salesforce, sfdcginus, track, wire

Post navigation

Previous Post: What is Wrapper Class in Salesforce Apex?
Next Post: LightningAlert, LightningConfirm, and LightningPromt in Lightning web components.

Leave a Reply Cancel reply

Your email address will not be published. Required fields are marked *

Categories

  • Admin
  • Apex
  • Apex Class
  • Apex Triggers
  • Customization
  • Flow
  • Lightning Web Component
  • Ligthing
  • LWC
  • Recent Post
  • Salesforce Security

Tags

@api @track @wire Admin Apex Apex Class Apex Trigger api connectedCallback() decorators disconnectedCallback disconnectedCallback() errorCallback errorCallback() Flow How to check whether record values are changed or not in Apex Trigger how to create modal in lwc how to create popup in lwc lighting flow LightningAlert LightningConfirm LightningPrompt lightning web component lwc modal/popup in lwc Modal in lwc notification in lwc popup in lwc renderedCallback() Role Hierarchies Salesforce Salesforce Customization salesforce flow salesforce latest update salesforce layoff sfdc sfdcGenius sfdcginus track trailhead Trigger.NewMap Vs Trigger.oldMap trigger.old vs Trigger.New wire wrapper class in apex wrapper class in salesforce

Resources

  • About Us
  • Blog
  • Contact Us
  • Home
  • Newsletter
  • Privacy Policy

Social Media

  • linkedIn
  • twitter

Copyright © 2025 sfdcGenius.

Powered by PressBook WordPress theme