function readOnly(count){ }
Starting November 20, the site will be set to read-only. On December 4, 2023,
forum discussions will move to the Trailblazer Community.
+ Start a Discussion
JonathanFox UKJonathanFox UK 

Communicate with LWC outside of hierarchy

Hi there,

I know it is possible to call a function on a child LWC by anotating the function with @api and using a querySelector to find the child BUT is it possible to do similar for a LWC which is not nested and not in the hierarchy?
Best Answer chosen by JonathanFox UK
AnudeepAnudeep (Salesforce Developers) 
As far as I know this can be done only within the hierarchy as of now. 
To communicate down the containment hierarchy, owner and parent components can call JavaScript methods on child components. 

I recommend reviewing this post where a similar discussion is made

Let me know if this helps, if it does, please close the query by marking this answer as best. It may help others in the community. Thank You!

All Answers

AnudeepAnudeep (Salesforce Developers) 
As far as I know this can be done only within the hierarchy as of now. 
To communicate down the containment hierarchy, owner and parent components can call JavaScript methods on child components. 

I recommend reviewing this post where a similar discussion is made

Let me know if this helps, if it does, please close the query by marking this answer as best. It may help others in the community. Thank You!
This was selected as the best answer
SwethaSwetha (Salesforce Developers) 
HI JonathanFox,

It is not possible to communicate with LWC outside of the hierarchy. The recommended approach would be to use Lightning Message Service(https://developer.salesforce.com/docs/component-library/bundle/lightning-message-service/documentation) in such a decoupled scenario.

Hope this helps you. Please mark this answer as best so that others facing the same issue will find this information useful. Thank you