Conditional rendering is a technique for displaying components or elements based on a predetermined condition. For example, conditional rendering is used if you want to display different messages at different times. We can render different LWC components or elements if a condition using conditional rendering in LWC is met.
In today’s blog, we will show you how to use conditional rendering in the lightning web component.
So, Let’s begin!
conditionalRendering.html
The ConditionalRendering class sets the value of the is SystemAdministrator property when a page loads.
If the is SystemAdministrator property is true, the if: true directive displays the nested template. You are now a System Administrator!
If the is SystemAdministrator property is false, the if: true directive displays the next nested template. You have successfully registered as a Business User!
Code
<template> | |
<lightning-card title="Conditional Rendering Example" icon-name="standard:user"> | |
<div class="slds-m-around_medium"> | |
<template if:true={isSystemAdministrator}> | |
<div class="slds-m-vertical_medium"> | |
Welcome, you are a System Administrator! | |
</div> | |
</template> | |
<template if:false={isSystemAdministrator}> | |
<div class="slds-m-vertical_medium"> | |
Welcome, you are a Business User! | |
</div> | |
</template> | |
</div> | |
</lightning-card> | |
</template> | |
conditionalRendering.js
We are not manipulating the DOM in this JavaScript; instead, we are importing the profile name from Salesforce and changing the value of the is SystemAdministrator property.
Code
import { LightningElement, wire, track } from 'lwc'; | |
import { getRecord } from 'lightning/uiRecordApi'; | |
import Id from '@salesforce/user/Id'; | |
import ProfileName from '@salesforce/schema/User.Profile.Name'; | |
export default class ConditionalRendering extends LightningElement { | |
userId = Id; | |
isSystemAdministrator = false; | |
@wire(getRecord, { recordId: Id, fields: [ProfileName] }) | |
userDetails({ error, data }) { | |
if (error) { | |
this.error = error; | |
} else if (data) { | |
if (data.fields.Profile.value != null && data.fields.Profile.value.fields.Name.value=='System Administrator') { | |
this.isSystemAdministrator = true; | |
} | |
} | |
} | |
} |
conditionalRendering.js-meta.xml
This configuration file makes the component available for all Lightning page types except the home page, which is only supported on desktops.
Code
<?xml version="1.0" encoding="UTF-8"?> | |
<LightningComponentBundle xmlns="http://soap.sforce.com/2006/04/metadata"> | |
<apiVersion>54.0</apiVersion> | |
<isExposed>true</isExposed> | |
<targets> | |
<target>lightning__HomePage</target> | |
</targets> | |
<targetConfigs> | |
<targetConfig targets="lightning__HomePage"> | |
<supportedFormFactors> | |
<supportedFormFactor type="Large" /> | |
</supportedFormFactors> | |
</targetConfig> | |
</targetConfigs> | |
</LightningComponentBundle> |
Output:
- if a User with a profile System Administrator loads the home page, the lightning web component will display the following message.
- if a User with a profile other than System Administrator loads the home page, the lightning web component will display the following message.
Conclusion
We hope you find the tutorial helpful. Feel free to use the code to implement conditional rendering in LWC.
Also, stay hooked to our tutorials as we will be back with another interesting Salesforce development solution soon. Catch you on the next one, Happy Coding!!