ExamGecko
Question list
Search
Search

List of questions

Search

Question 205 - Certified B2B Commerce Administrator discussion

Report
Export

Which two methods should a developer implement in a Lightning web component to 03m 355

successfully handle the subscription lifecycle of a Message Channel?

A.
Subscribe()
Answers
A.
Subscribe()
B.
stopListener()
Answers
B.
stopListener()
C.
startListener()
Answers
C.
startListener()
D.
unsubscribe()
Answers
D.
unsubscribe()
Suggested answer: A, D

Explanation:

To handle the subscription lifecycle of a Message Channel, a developer should implement two methods in a Lightning web component: subscribe() and unsubscribe(). The subscribe() method is used to register a callback function that handles the messages published on the message channel. The unsubscribe() method is used to remove the subscription and stop receiving messages.Both methods are part of the Lightning Message Service API, which is imported from the lightning/messageService module1. For example:

// Import the Lightning Message Service API import { subscribe, unsubscribe, MessageContext } from 'lwc/messageService';

// Declare a message context @wire(MessageContext) messageContext;

// Declare a subscription variable subscription = null;

// Subscribe to the message channel in the connected callback connectedCallback() { this.subscribeToMessageChannel(); }

// Subscribe to the message channel using the LMS API subscribeToMessageChannel() { if (!this.subscription) { this.subscription = subscribe( this.messageContext, MESSAGE_CHANNEL, // The name of the message channel (message) => this.handleMessage(message) // The callback function ); } }

// Unsubscribe from the message channel in the disconnected callback disconnectedCallback() { this.unsubscribeFromMessageChannel(); }

// Unsubscribe from the message channel using the LMS API unsubscribeFromMessageChannel() { unsubscribe(this.subscription); this.subscription = null; }

asked 23/09/2024
harinder giri
33 questions
User
Your answer:
0 comments
Sorted by

Leave a comment first