Newer Version Available

This content describes an older version of this product. View Latest

lightning__tabFocused

A Lightning message channel that corresponds to the lightning:tabFocused Aura app event. This message channel is available for Lightning web components used within a Lightning console app.

By default, this event is only received when that component's tab comes into focus, not when it leaves focus. To receive all events and minimize performance impact, use a utility item as the only listener.

Response

The response is the same as that of the lightning:tabFocused Aura app event.

LWC Example

Import the lightning__tabFocused message channel from the @salesforce/messageChannel/ scoped module. The event returns the message in the response.

1import { LightningElement, track, wire } from "lwc";
2import { MessageContext, subscribe, unsubscribe, APPLICATION_SCOPE } from "lightning/messageService";
3import tabFocusedChannel from "@salesforce/messageChannel/lightning__tabFocused";
4
5export default class TabFocusedExample extends LightningElement {
6    subscription = null;
7    @wire(MessageContext) messageContext;
8
9    // Encapsulate logic for Lightning message service subscribe and unsubscribe
10    subscribeToMessageChannel() {
11        if (!this.subscription) {
12            this.subscription = subscribe(
13                this.messageContext,
14                tabFocusedChannel,
15                (message) => this.handleMessage(message),
16                { scope: APPLICATION_SCOPE }
17            );
18        }
19    }
20
21    unsubscribeToMessageChannel() {
22        unsubscribe(this.subscription);
23        this.subscription = null;
24    }
25
26    // Handler for message received by component
27    handleMessage(message) {
28        // do something
29    }
30    // Standard lifecycle hooks used to subscribe and unsubscribe to the message channel
31    connectedCallback() {
32        this.subscribeToMessageChannel();
33    }
34
35    disconnectedCallback() {
36        this.unsubscribeToMessageChannel();
37    }

For more information, see Subscribe and Unsubscribe from a Message Channel.