Svelte: How do I *manually* forward a DOM event?

Created on 24 Oct 2019  路  2Comments  路  Source: sveltejs/svelte

I know I can automatically forward a component event by just on:myEvent.
I know I can manually forward a component event (CustomEvent) by

on:myEvent={(event) => {
  doSomethingWith(event);
  createEventDispatcher()('myEvent', event.detail);
}}

I know I can automatically forward a DOM event by just on:click.
However, I don't how how to manually forward a DOM event, as in

on:click={(event) => {
  doSomethingWith(event);
  // WHAT DO I DO HERE?
}}
question

All 2 comments

There's not a way to manually emit a DOM event from a component. What the forwarding on:click does internally is to directly call all of the component event handlers.

One thing you could do is to attach two handlers for the event on the element, one two forward the event and one to handle it. <button on:click={doSomethingWith} on:click>. Which one is positioned first in the arguments is the one that will happen first, if that's important for your application.

Ah... I didn't know I can put multiple handlers on the same event. Thanks.

Was this page helpful?
0 / 5 - 0 ratings

Related issues

juniorsd picture juniorsd  路  3Comments

1u0n picture 1u0n  路  3Comments

rob-balfre picture rob-balfre  路  3Comments

thoughtspile picture thoughtspile  路  3Comments

mmjmanders picture mmjmanders  路  3Comments