Check Whether a Lightning Web Component Is Connected to the DOM

Use a property, isConnected, to check whether a component is connected to the Document Object Model (DOM).

Where: This change applies to Lightning web components in Lightning Experience and all versions of the Salesforce app.

Why: You can already use the 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 a more ergonomic developer experience.

How: Lightning web components now reflect 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.
           }
        })
    }
}
Tip

Tip

Lightning web components reflect the properties of other Web APIs as well. For a complete list, see Web API Properties in the Lightning Web Components Developer Guide.