To let Web Components communicate with each other, I'm using custom events. Let's imagine the following:
WebComponentA uses or contains WebComponentB which sends a CustomEvent (bubbles: true, composed: true) if a button is clicked. WebComponentA wants to do something if WebComponentB sends this event.
How should I dispatch the event within WebComponentB?
window.dispatchEvent(customEvent);
or
this.shadowRoot.dispatchEvent(customEvent);
How should I catch the event within WebComponentA?
window.addEventListener(custom-event, () => {
);
or
this.shadowRoot.addEventListener(custom-event, () => {
);
Are there any negative effetcts I should consider using one or another?
Thank you!