DEV Community

Discussion on: `composed: true` considered harmful?

Collapse
 
trystan2k profile image
Thiago Mendonca

Hi Westbrook, thanks for the article, it helped me to clarify a lot of things !

I have a case where I think I would not need composed:true, if I understood correctly.

Imagine I have a component (A), that in its shadow DOM, has 4 (1, 2. 3, 4) other components, each of them dispatching its own event. If for A to dispatch its event, it needs that the 4 children components to have fired their events (to compose a payload, for example).

 <main-component>
     <children1-component @click=${child1Handler]></children1-component>
     <children2-component @click=${child2Handler]></children2-component>
     <children3-component @click=${child3Handler]></children3-component>
     <children4-component @click=${child4Handler]></children4-component>
</main-component>
Enter fullscreen mode Exit fullscreen mode

So, in this case, can (should?) the click events of the children components have composed: false, as I don't care or want them outside main-component ?

Collapse
 
westbrook profile image
Westbrook Johnson

Absolutely, this is a great case for not using composed: true as it appears that you are talking about the internal implementation of your element as opposed to its public API. Your child components will be able to limit possible side effects of the events they are dispatching while reducing the overall DOM those events traverse in anticipation for an escaping event being dispatched on the main component. Even at that point there could be room for containing that event to the scope of the tree in which the main component is present.

If you found this introduction to event theory useful, you might also check out open-wc.org/faq/events.html where the capabilities outlined here are converted to recommendations that should prove useful in organizing your components/applications.

Collapse
 
trystan2k profile image
Thiago Mendonca

Thanks ! Definitely will look at it !