Tuesday, June 23, 2020

How to check Lightning Web Component(lwc) Is Connected to the DOM?

we have already connectedCallback() and disconnectedCallback() lifecycle hooks to react when a component is connected to and disconnected from the DOM. However, using the Node.isConnected property is more ergonomic developer experience.

The Node.isConnected property, which returns a Boolean indicating whether the component is connected to the DOM. Simply use this.isConnected in a component’s JavaScript.

import { LightningElement } from 'lwc';
export default class MyComponent extends LightningElement {
    someMethod() {
        const cmp = this;
        const aPromise = new Promise(tetherFunction);
        aPromise.then((result) => {
           if (cmp.isConnected) {
               // Update the component with the result.
           } else {
               // Ignore the result and maybe log.
           }
        })
    }
}

No comments:

Post a Comment