I'm confused about what documentation says about event best practices.


To communicate data to elements in the same shadow tree, don’t add myProperty into event.detail. The consumer of the event can use the event.target.myProperty. To communicate data to elements that aren’t in the same shadow tree, use event.detail.

Doesn't these sentences contradict one another? In both instances you're supposed to use event.detail. What is the difference they are trying to convey here?

  • 2
    :D Indeed, it is a little contradictory! From what I saw in the code provided by Salesforce, they use the event.detail.myProperty solution. – Martin Lezer Oct 28 '19 at 8:36
  • 1
    @Martin Lezer So one should use CustomEvent.detail for passing data and Event.target just for launching an event? – Neophyte__c Oct 28 '19 at 9:28
  • 2
    My bad, I confused myself with the documentation.... In fact, Salesforce uses the event.detail property to transmit the custom data. They don't add the myProperty to the detail. For exemple, here's how they raise a custom event: new CustomEvent('select', { detail: this.contact.Id }); And here's how they handle it: const contactId = event.detail; – Martin Lezer Oct 28 '19 at 9:51

This statement is saying that when you are trying to communicate in the same component js file, then there is no need to add event.detail as the consumer can access anything from event object like event.target.name or event.target.value. But, if you are trying to communicate with 2 different components, parent and child or sibling components then you have to provide the event.detail with some object as {'name':'Mayank',age:'25'} because the consumer will not have access to event.target properties. Thanks

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.