Posts

Showing posts with the label Lightning Web Components

LWC Enhancements for Developers | Spring '23 Edition

Image
There are plenty of new Lightning Web Components features to get excited about in the Spring ’23 release.  Today I will share 4 essential features in Lightning Web Components that will ease our job as Salesforce developers. 1. Query DOM elements with refs Now you can use refs to access elements in shadow DOM and light DOM. Refs locate DOM elements without a selector and only query elements contained in a specified template. Previously, you could only use querySelector() to locate specific DOM elements. Before: <template> <div>My Div</div> </template> To query DOM elements in JavaScript, we use the this.template object. If we have multiple div elements in the markup, this method requires a little more code. renderedCallback() { let myDiv = this.template.querySelector('div'); } After: First, add the lwc:ref directive to your element and assign it a value. To call that reference, use this.refs. In this example, the <div> element has the directi