Open jpchase opened 4 years ago
Another recommended pattern:
Event subclasses let you localize all the state of the event - the data, name, event options - so that it can't diverge per dispatch call. Here's my pattern:
class FooClickedEvent extends Event { static eventName = 'foo-clicked'; fooData: FooData; constructor(fooData: FooData) { super(FooClickedEvent.eventName, {bubbles: true, composed: true}); this.fooData = fooData; } }
this.dispatchEvent(new FooClickedEvent(fooData));
See: https://github.com/Polymer/lit-element/issues/808 https://gist.github.com/difosfor/ceeb01d03a8db7dc68d5cd4167d60637 https://github.com/Polymer/lit-element/issues/896 https://43081j.com/2020/11/typed-events-in-typescript