6

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.
3
  • Can you please explain what is your use case? Mar 2, 2021 at 9:17
  • For example we have two data tables. One for search records and second which stores all selected records. Mar 2, 2021 at 9:24
  • 4
    Both options are valid but IMO it depends on your use case for each child component. Eg. If you will always use the child components as child components and not as standalone components I would use a custom event and pass values via the parent. But if the child components will act as stand alone - LMS is your better option
    – sfdx bomb
    Mar 2, 2021 at 10:25

1 Answer 1

5

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.

You must log in to answer this question.

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