[SalesForce] Is it possible to fire an event from a LWC and handle in another LWC which is not in containment hierarchy

Is it possible to fire an event from a lightning web component and handle in another lightning web component which is not in containment hierarchy? This is achievable in Aura component using application event.

Best Answer

Yes. It can be achieved using the pub-sub model.

To communicate between components that aren’t in the same DOM tree, use a singleton library that follows the publish-subscribe pattern.

Refer to Communicate Between Components documentation for more details.

For example, if you add two components to a Lightning page in Lightning App Builder, use the pubsub module to send events between them.

You can find the pubsub module here on lwc-recipe

Related Topic