ExamGecko
Question list
Search
Search

List of questions

Search

Question 209 - Certified B2B Commerce Administrator discussion

Report
Export

A developer is building a custom component in Lightning web components (LWC) that has a grandchild component that needs to pass information to the grandparent component. Ex Ther m Ss

What is the correct way to demonstrate the passing of a message from the grandchild component to the grandparent component?

A)

B)

C)

D)

A.
Option A
Answers
A.
Option A
B.
Option B
Answers
B.
Option B
C.
Option C
Answers
C.
Option C
D.
Option D
Answers
D.
Option D
Suggested answer: A

Explanation:

The correct way to demonstrate the passing of a message from the grandchild component to the grandparent component is by using an attribute on the child component to pass the message. This can be done by using the @api decorator in the grandchild component and then handling the message in the grandparent component with the handlemessage method. For example:

<!-- grandchild.html --> <template> <lightning-button label=''Send Message'' onclick={handleClick}></lightning-button> </template>

// grandchild.js import { LightningElement, api } from 'lwc';

export default class Grandchild extends LightningElement { // Declare an attribute to pass the message @api message = 'Hello from grandchild';

// Handle the button click and fire an event handleClick() { // Create a custom event with the message as detail const event = new CustomEvent('message', { detail: this.message }); // Dispatch the event from this component this.dispatchEvent(event); } }

<!-- child.html --> <template> <c-grandchild onmessage={handleMessage}></c-grandchild> </template>

// child.js import { LightningElement } from 'lwc';

export default class Child extends LightningElement { // Handle the message event from the grandchild component handleMessage(event) { // Get the message from the event detail const message = event.detail; // Fire another event with the same message to pass it to the parent component this.dispatchEvent(new CustomEvent('message', { detail: message })); } }

<!-- parent.html --> <template> <c-child onmessage={handleMessage}></c-child> </template>

// parent.js import { LightningElement } from 'lwc';

export default class Parent extends LightningElement { // Declare a property to store the message message;

// Handle the message event from the child component handleMessage(event) { // Get the message from the event detail and assign it to the property this.message = event.detail; // Do something with the message, such as display it on the screen console.log(this.message); } }

asked 23/09/2024
mostafa khalaf
53 questions
User
Your answer:
0 comments
Sorted by

Leave a comment first