-
-
Notifications
You must be signed in to change notification settings - Fork 4.5k
Svelte 5: event handling makes it harder to understand #10645
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Comments
This is what I've been doing.
Have that in a listener.ts file. Then in my components I can just call A) uses native JavaScript events so you can pass details as high up as you want. B) will be compatible with svelte unless they remove the use:action directives. |
This only works for events on HTML elements. Even then, it looks terribly awkward. I think it shows even more, why we need |
The advantages (spreading events, more flexibility and power how to compose/use them, no |
Just for completion sake, I'd like to go over the supposed advantages:
As I said above, it does decrease the learning curve, but at the cost of readability and easily understood conventions. I'm pretty sure the new convention in most people's code will be: "If it's a function, call it Examples of how users already don't follow this convention:
The extra boilerplate is now in defining the types of the callbacks.
Fully agree. No need for
This could be done with a variant of the <script lang="ts">
const myEvents = $events.pass<{
a: [number]
}>()
// Without typescript:
// const myEvents = $events.pass("a")
const otherEvents = $events.pass<{
b: [string]
}>()
// const otherEvents = $events.pass("b")
</script>
<My.Component on:myEvents />
<Other.Component on:otherEvents /> Honestly, this is even better than what is suggested by Svelte 5 currently, because this doesn't assume you want to pass all of the event handlers to a single other component. EDIT: Also, if
<script lang="ts">
import { HTMLInputProps } from "svelte/elements"
const { inputAttrs = {} } = $props<{
inputAttrs: HTMLInputProps
}>
const emit = $events<{
commit: [],
}>()
</script>
<input {...inputAttrs} />
<button on:click|preventDefault={() => emit("commit")}>Commit</button>
This could be easily implemented: <script lang="ts">
const emit = $events<{
handle: []
}>()
</script>
{#if emit.handle}
<!-- Do your thing -->
{/if} This would mean that
Event handlers are by definition optional. That's also how HTML works. There are no required event handlers in HTML.
I don't understand this one. Examples of other users who don't like the removal of
|
Uh oh!
There was an error while loading. Please reload this page.
Describe the problem
I'd argue, that getting rid of the concept of events in Svelte 5 increases the complexity, rather than decreasing it. It's true, that it's one less thing to learn, but events also enforce a specific component design pattern:
Just by looking at this piece of code, we now that
doHandle
returnsvoid
, or at least, the components discards the return value ofdoHandle
. Also we know, thatdoHandle
will not be waited for in the case it is async.If we do it the Svelte 5 way:
All of this implicit knowledge on how
onAction
operates is lost. We need to look at the code ofMy.Component
. All of this can be remedied by documentation, but it's to have this knowledge without having to look at docs.Additionally, when creating the props for event handlers, more typing boilerplate is required.
Describe the proposed solution
Reintroduce explicit events with the
$events
rune. I think it could have a similiar api as Vue'sdefineEmits
.This would behave the same as:
Importance
would make my life easier
The text was updated successfully, but these errors were encountered: