0

Quick theoritical question.

I've read about the lightning message service communication between Lightning Web Components and it was specified that this type of communication is between unrelated components.

My question is, what does unrelated mean and how is it determined?

If a Lightning Web Component 1 calls Lightning Web Component 2 through its template, is there a Parent to Child communication automatically implied? Do they have to be nested basically? Because I'm pretty sure I saw an example of LMS for that scenario and it confused me.

How to separate these two? Not talking about technicalities.

'Kay maybe not so quick question after all. Thanks in advance!

1 Answer 1

1

Any component a parent can get direct access to, it can communicate with, and a child can communicate with its direct parent and, optionally, other elements directly in between itself and its direct parent (using { bubbles: true, composed: false }). You can read more in Configure Event Propagation.

For anything else, including siblings, cousins, etc, you need either a common ancestor to facilitate communication between siblings, etc, or you can use LMS. LMS is particularly useful if there is no common parent you can leverage (e.g. two components on a Lightning Page), but you can use it to arbitrarily communicate between any two components, even if they are Aura and LWC, or even if one of them is in a Visualforce page.

You can certainly use LMS for parent-child communication, too, but this is probably overkill, as a simple @api function on the child works just as well. The example you viewed was probably intentionally simple, but LMS can certainly deal with third cousin twice removed components as easily as a parent-child relationship.

1
  • Thanks for taking the time to explain, it is much more clear now. Also, I didn't know about event propagation so that's new on my plate. Cheers! Commented May 8, 2021 at 8:23

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .