{"id":2819,"date":"2020-06-01T00:57:13","date_gmt":"2020-06-01T06:27:13","guid":{"rendered":"https:\/\/www.emizentech.com\/blog\/?p=2819"},"modified":"2022-01-21T11:43:49","modified_gmt":"2022-01-21T11:43:49","slug":"properties-in-lightning-web-component","status":"publish","type":"post","link":"https:\/\/multisitelocal.ezxdemo.com\/blog\/properties-in-lightning-web-component.html","title":{"rendered":"How To Use Properties In Lightning Web Component Salesforce"},"content":{"rendered":"<p>In LWC properties are similar to JavaScript properties. In js file of component you can declare your properties using camel casing and in your Html you can write same name using kebab casing called attribute this way both will be mapped to each other. Ex : In js file I have a property as showFeature and its corresponding attribute in Html file written as show-feature . This way both will be mapped to each other and value of property will be visible in html.<\/p>\n<p>There are two types of properties in LWC<\/p>\n<ul>\n<li>Reactive Properties<\/li>\n<li>Non-reactive Properties<\/li>\n<\/ul>\n<p>Before starting with this lets understand with decorators<\/p>\n<h2><span class=\"ez-toc-section\" id=\"What_is_decorator\"><\/span>What is decorator?<span class=\"ez-toc-section-end\"><\/span><\/h2>\n<p>Decorator is a part of ECMA script and used to add extra functionality in your function or methods. In LWC we use 3 decorator to enhance the functionality of our property or function.<br \/>\n@track<br \/>\n@api<br \/>\n@wire<\/p>\n<h2><span class=\"ez-toc-section\" id=\"Reactive_Properties\"><\/span>Reactive Properties<span class=\"ez-toc-section-end\"><\/span><\/h2>\n<p>If value of any reactive property changes and that is used in any template then that template will re-render and it will evaluate again all the expression written in template. It can be of two type.<\/p>\n<ul>\n<li>Public properties<\/li>\n<li>Private properties<\/li>\n<\/ul>\n<h4>Also Read: <a class=\"row-title\" href=\"https:\/\/multisitelocal.ezxdemo.com\/blog\/data-loader-vs-import-wizard.html\" target=\"_blank\" aria-label=\"\u201cDifference: Data Loader vs Import Wizard In Salesforce\u201d (Edit)\" rel=\"noopener\">Difference: Data Loader vs Import Wizard<\/a><\/h4>\n<h3><span class=\"ez-toc-section\" id=\"Public_Properties\"><\/span>Public Properties<span class=\"ez-toc-section-end\"><\/span><\/h3>\n<p>In order to expose any property as public we decorate it as @api. Along with decoration you have to import this in your js file as<\/p>\n<p><code class=\"d-block\">import { LightningElement, api } from 'lwc';<\/code><\/p>\n<p>These public properties can be used by the parent component. The component that set a property as public can set only its default value. It&#8217;s not like you can\u2019t change but it&#8217;s preferable that parent change and play with its value.<\/p>\n<div class=\"center-imgs\"><a href=\"https:\/\/multisitelocal.ezxdemo.com\/contact-us.html?utm_source=blog&amp;utm_medium=banner&amp;utm_campaign=emizen_blog\"><img fetchpriority=\"high\" decoding=\"async\" class=\"aligncenter wp-image-2389 size-full\" src=\"\/blog\/wp-content\/uploads\/sites\/2\/2020\/05\/hire-salesforce-developers-1.png\" alt=\"hire salesforce developers\" width=\"1000\" height=\"200\" \/><\/a><\/div>\n<h3><span class=\"ez-toc-section\" id=\"Private_Properties\"><\/span>Private Properties<span class=\"ez-toc-section-end\"><\/span><\/h3>\n<p>In order to expose any property as public we decorate it as @api. Along with decoration you have to import this in your js file as<\/p>\n<p><code class=\"d-block\">import { LightningElement, track } from 'lwc';<\/code><\/p>\n<p>These can be used by the component in which it is defined.<\/p>\n<\/div>\n<p><strong>Let&#8217;s understand the difference between public and private with the help of an example.<\/strong><\/p>\n<p>Create a web component named parentEx.<\/p>\n<p>In parentEx.html<\/p>\n<pre>&lt;template&gt;\r\n    &lt;lightning-card title='Properties'&gt;\r\n        Your Birthdate is : (Tracked property) :  {inputDate} &lt;br\/&gt;\r\n    &lt;\/lightning-card&gt;\r\n&lt;\/template&gt;\r\n<\/pre>\n<p>parentEx.js<\/p>\n<pre>import { LightningElement, api } from 'lwc';\r\n\r\nexport default class ParentEx extends LightningElement {\r\n    @api inputDate = '13 December';\r\n   \/\/ a non reactive property\r\n    \/\/ dateVal = 'Date is : ';\r\n    \r\n   \r\n}\r\n<\/pre>\n<p>Create another web component named superParentEx. Leave its js file as it is.<br \/>\nIn superParentEx.html<\/p>\n<pre>&lt;template&gt;\r\n    Super Parent Example : \r\n    &lt;c-parent-Ex input-date='24 March (Set by parent )'&gt;&lt;\/c-parent-Ex&gt;\r\n&lt;\/template&gt;\r\n<\/pre>\n<p>Now add the superParentEx component in your view or in aura application and check the result. You will see the value of inputDate will be visible as set in parentEx component.<\/p>\n<p>Now change track with api in js file of parentEx component and check the result again. You will see now superParentEx is able to change the value of inputDate property now.<\/p>\n<p>For <a href=\"https:\/\/multisitelocal.ezxdemo.com\/salesforce-consulting.html\">salesforce consulting<\/a> of your project get in touch with our team and hire us for <a href=\"https:\/\/multisitelocal.ezxdemo.com\/salesforce.html\">salesforce development services<\/a>.<\/p>\n<h4 class=\"blog-text\">Learn More: <a class=\"row-title\" href=\"https:\/\/multisitelocal.ezxdemo.com\/blog\/call-apex-method-in-lightning-web-components.html\" aria-label=\"\u201cHow To Call Apex Method In Lightning Web Components In Salesforce\u201d (Edit)\">How To Call Apex Method In Lightning Web Components<\/a> and <a class=\"row-title\" href=\"https:\/\/multisitelocal.ezxdemo.com\/blog\/create-salesforce-lightning-map-component.html\" target=\"_blank\" aria-label=\"\u201cHow to Create a Salesforce Lightning Map Component?\u201d (Edit)\" rel=\"noopener\">How to Create a Salesforce Lightning Map Component?<\/a><\/h4>\n","protected":false},"excerpt":{"rendered":"<p>In LWC properties are similar to JavaScript properties. In js file of component you can declare your properties using camel casing and in your Html you can write same name using kebab casing called attribute this way both will be mapped to each other. Ex : In js file I have a property as showFeature<\/p>\n","protected":false},"author":39,"featured_media":3020,"comment_status":"closed","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"MSN_Categories":"Uncategorized","MSN_Publish_Option":false,"MSN_Is_Local_News":false,"MSN_Is_AIAC_Included":"Empty","MSN_Location":"[]","MSN_Add_Feature_Img_On_Top_Of_Post":false,"MSN_Has_Custom_Author":false,"MSN_Custom_Author":"","MSN_Has_Custom_Canonical_Url":false,"MSN_Custom_Canonical_Url":"","_lmt_disableupdate":"","_lmt_disable":"","footnotes":""},"categories":[87],"tags":[],"class_list":{"0":"post-2819","1":"post","2":"type-post","3":"status-publish","4":"format-standard","5":"has-post-thumbnail","7":"category-salesforce-development"},"modified_by":"emizentech","featured_image_src":"https:\/\/multisitelocal.ezxdemo.com\/blog\/wp-content\/uploads\/sites\/2\/2020\/06\/How-to-use-Properties-In-LWC-min-1.jpg","featured_image_src_square":"https:\/\/multisitelocal.ezxdemo.com\/blog\/wp-content\/uploads\/sites\/2\/2020\/06\/How-to-use-Properties-In-LWC-min-1.jpg","author_info":{"display_name":"Virendra Sharma","author_link":"https:\/\/multisitelocal.ezxdemo.com\/blog\/author\/salesforce"},"_links":{"self":[{"href":"https:\/\/multisitelocal.ezxdemo.com\/blog\/wp-json\/wp\/v2\/posts\/2819","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/multisitelocal.ezxdemo.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/multisitelocal.ezxdemo.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/multisitelocal.ezxdemo.com\/blog\/wp-json\/wp\/v2\/users\/39"}],"replies":[{"embeddable":true,"href":"https:\/\/multisitelocal.ezxdemo.com\/blog\/wp-json\/wp\/v2\/comments?post=2819"}],"version-history":[{"count":0,"href":"https:\/\/multisitelocal.ezxdemo.com\/blog\/wp-json\/wp\/v2\/posts\/2819\/revisions"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/multisitelocal.ezxdemo.com\/blog\/wp-json\/wp\/v2\/media\/3020"}],"wp:attachment":[{"href":"https:\/\/multisitelocal.ezxdemo.com\/blog\/wp-json\/wp\/v2\/media?parent=2819"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/multisitelocal.ezxdemo.com\/blog\/wp-json\/wp\/v2\/categories?post=2819"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/multisitelocal.ezxdemo.com\/blog\/wp-json\/wp\/v2\/tags?post=2819"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}