PDI · Question #142
A developer created a child Lightning web component nested inside a parent Lightning web component, parent component needs to pass a string value to the child component. In which two ways can this be
The correct answer is A. The parent component can use a custom event to pass the data to the child component, D. The parent component can use a public property to pass the data to the child component.. To pass a string value from a parent Lightning web component to a child, developers can use public properties on the child, or the parent can dispatch a custom event which the child is configured to listen for.
Question
A developer created a child Lightning web component nested inside a parent Lightning web component, parent component needs to pass a string value to the child component. In which two ways can this be accomplished? Choose 2 answers
Options
- AThe parent component can use a custom event to pass the data to the child component,
- BThe parent component can use the Apex controller class to send data to the child component.
- CThe parent component can invoke a method in the child component
- DThe parent component can use a public property to pass the data to the child component.
How the community answered
(29 responses)- A93% (27)
- B3% (1)
- C3% (1)
Why each option
To pass a string value from a parent Lightning web component to a child, developers can use public properties on the child, or the parent can dispatch a custom event which the child is configured to listen for.
While less conventional for direct parent-to-child data flow, a parent component can dispatch a custom event, and a child component configured with an event listener (e.g., via `template.addEventListener`) can technically receive and process the event data.
The Apex controller class typically retrieves data from the server or performs DML operations; it is not a direct mechanism for passing data between a parent and child LWC on the client side.
Invoking public methods on a child component is primarily used to trigger actions or imperative logic rather than for simply passing and maintaining a string value for display or state, which is more directly handled by public properties.
The parent component can declare a public property on the child component in its HTML template, enabling a direct and reactive way to pass a string value from parent to child.
Concept tested: LWC parent-child communication
Source: null
Topics
Community Discussion
No community discussion yet for this question.