Salesforce Platform Developer I (legacy) — Question 256
A developer created a weather app that contains multiple Lightning web components that are in different DOM trees.
One of the components, called Toggle, has a toggle for Fahrenheit or Celsius units. Another component, called Temperature, displays the current temperature in the unit selected in the Toggle component.
When a user toggles from Fahrenheit to Celsius or vice versa in the Toggle component, the information must be sent to the Temperature component so the temperature can be converted and displayed.
What is the recommended way to accomplish this?
Answer options
- A. Create a custom event to handle the communication between components.
- B. The Toggle component should call a method in the Temperature component.
- C. Use Lightning Message Service to communicate between the components.
- D. Use an application event to communicate between the components.
Correct answer: A
Explanation
The correct answer is A because creating a custom event allows for effective communication between Lightning web components that are in different DOM trees. The other options are not suitable; option B suggests a direct method call, which is not possible across different DOM trees, while option C involves Lightning Message Service, which is not necessary for this scenario. Option D refers to application events, which are also less suitable compared to custom events in this case.