Note: This release is in preview. Features described here don’t become generally available until the latest general availability date that Salesforce announces for this release. Before then, and where features are noted as beta, pilot, or developer preview, we can’t guarantee general availability within any particular time frame or at all. Make your purchase decisions only on the basis of generally available products and features.
lightning__tabFocused
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.