CRT-450 practice question 2 of 30

A developer created a child Lightning web component nested inside a parent Lightning web component. The parent component needs to pass a string value to the child component. In which two ways can this be accomplished? Choose 2 answers

Full set: 201 questions
Answer options
A: The parent component can invoke a public method in the child component. Correct answer
B: The parent component can use a public property to pass the data to the child component, Correct answer
C: The parent can use the Apex controller class to send data to the child component.
D: The parent component can use a custom event to pass the data to the child component.
Correct Answer:
A. The parent component can invoke a public method in the child component.; B. The parent component can use a public property to pass the data to the child component,
Explanation

Communication from a parent Lightning Web Component (LWC) to a nested child LWC is accomplished through two primary mechanisms. The first is declaratively, by setting a public property on the child component. The child exposes a property using the @api decorator, which the parent can then set in its markup. The second is imperatively, where the parent component gets a reference to the child and calls a public method, also exposed via the @api decorator, passing the string value as an argument. Both are standard, documented patterns for parent-to-child data flow.

References

1. Salesforce Lightning Web Components Developer Guide: For passing data via public properties. The guide states

"To expose a public property

decorate a field with @api. Public properties are reactive. If the value of a reactive property changes

the component’s template rerenders."

Source: Lightning Web Components Developer Guide

"Pass Data from Parent to Child with Public Properties".

2. Salesforce Lightning Web Components Developer Guide: For invoking methods on a child. The guide explains

"To expose a public method

decorate it with @api. Public methods are part of a component's public API. To communicate down the containment hierarchy

a parent component can call a public method on a child component."

Source: Lightning Web Components Developer Guide

"Call Methods on Child Components".

3. Salesforce Lightning Web Components Developer Guide: To differentiate from child-to-parent communication. The guide specifies

"A component dispatches an event to send data to a component in its container hierarchy. The CustomEvent interface lets you create your own events." This confirms events flow upwards.

Source: Lightning Web Components Developer Guide

"Communicate with Events".

Scroll to Top