LWC IntroductionWhy Lightning Web ComponentLWC Browser supportSalesforce DX environment setupSalesforce DX project setupHello world using LWCOne-Way Data BindingTwo-Way Data Binding (@track)Conditional Renderingfor:each loopiterator loopRender multiple templateParent to Child Communication with string dataParent to Child Communication with Array/Object dataParent to Child Communication on action at parentParent to Child Communication by calling the Child method from Parent component.Child to Parent Communication by simple actionChild to Parent Communication by passing data on actionChild to Parent Communication by event bubbling
One-Way Data Binding
What is Data Binding?
Data binding in the Lightning web component is the synchronization between the controller and the template(HTML).
What is One-Way Data Binding?
One-way data binding is a situation where information flows in only one direction in our case from the controller to the template(HTML)
Note - After Spring ’20 release- All fields in a Lightning web component class are reactive. If a field’s value changes, and the field is used in a template or in a getter of a property that’s used in a template, the component rerenders and displays the new value.
Example of One-Way Data Binding
- In VS Code, open the Command Palette by pressing
Ctrl+Shift+Pon Windows orCmd+Shift+Pon macOS. - Type
SFDXand SelectSFDX: Create Lightning Web Component. - Type
dataBindingas the name of the new component and pressEnter. - Again, Press
Enterto accept the defaultforce-app/main/default/lwc. - Goto your
lwcfolder, you will see one new component with the namedataBindinggets created. - Let's add the following code to
dataBinding.html,dataBinding.jsanddataBinding.js-meta.xml
dataBinding.html
<template>
<lightning-card title="One-way Data Binding Demo" icon-name="custom:custom1">
<hr />
<div class="slds-p-around_medium">
<h1>My full name is {fullname}</h1>
</div>
</lightning-card>
</template>- Inside the
<template>, we have used thelightning-card - Inside the
lightning-cardWe have created a<h1>tag with text and propertyfullnamebinding. {fullname}- To bind a property in a template we have to surround the property with curly braces:
dataBinding.js
import { LightningElement } from "lwc";
export default class DataBinding extends LightningElement {
fullname = "Salesforce Troop";
}- In the first line, we are importing
LightningElementfromlwcmodule - After that, we are creating a
classDataBinding(Note - the class name always be pascal case) - Within the
class, we have to define a propertyfullnameand assign a valueSalesforce Troopto it. - So when a component loads, it initialize the variable
fullnamewith valueSalesforce Troop. After that, when HTML starts rendering in the browser, lwc engine looks for the{}tags and replace thefullnameinside the curly braces with the properties defined inside the classDataBinding.
dataBinding.js-meta.xml
<?xml version="1.0" encoding="UTF-8"?>
<LightningComponentBundle xmlns="http://soap.sforce.com/2006/04/metadata" fqn="dataBinding">
<apiVersion>46.0</apiVersion>
<isExposed>true</isExposed>
<targets>
<target>lightning__AppPage</target>
<target>lightning__RecordPage</target>
<target>lightning__HomePage</target>
</targets>
</LightningComponentBundle>dataBinding.js-meta.xmlis a configuration file- The configuration file defines the metadata values for the component, including the design configuration for Lightning App Builder and Experience Builder.
- This file only show your component in app builder
Final Output
After placing the component on the page, you will see the following output.