[SalesForce] Communication between two child LWC Components

What is the best way to realize bi-directional communication between two LWC components. Is it better approach to pass values throught parent component or use Lightning message service?

  1. Dispatch CustomEvent on one child component to parent and pass that value to public (@api) property on another child component.
  2. Both components publish and subscribe at the same message channel.

Best Answer

In most cases, it is preferable to use props/events over pub/sub when communicating between sibling components.

The main issue with pub/sub is that it creates an implicit coupling between the child component. This implicit coupling makes the child components less reusable and harder to test.

For more information on this topic, I would recommend checking out the Step up your LWC skills - part 1. Disclaimer, I am the author of this blog post.

Related Topic