Skip to content
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

New rule: prefer-event-forwarding #850

Open
mcmxcdev opened this issue Sep 11, 2024 · 1 comment
Open

New rule: prefer-event-forwarding #850

mcmxcdev opened this issue Sep 11, 2024 · 1 comment
Labels
enhancement New feature or request new rule

Comments

@mcmxcdev
Copy link

Motivation

In our dev team, some devs tend to redefine event handlers as props which makes code more verbose than it needs to be.

Button.svelte

export let onClick = () => {}
...

<button on:click={onClick}>Click me</button>

Instead, we can rely on event forwarding like this:

Button.svelte

// no onClick prop
...

<button on:click>Click me</button>

See more: https://learn.svelte.dev/tutorial/event-forwarding

Description

The rule should report on props that mirror native HTML event handlers and suggest simplifying them with event forwarding.

Examples

<!-- ✓ GOOD -->
<button on:click>Click me</button>

<!-- ✗ BAD -->
<script>
export let onClick = () => {}
</script>

<button on:click={onClick}>Click me</button>

Additional comments

No response

@mcmxcdev mcmxcdev added enhancement New feature or request new rule labels Sep 11, 2024
@bfanger
Copy link

bfanger commented Sep 16, 2024

Svelte 5 is moving into direction of using props for event handlers:

Instead of doing to 'forward' the event from the element to the component, the component should accept an onclick callback prop:

<script>
  let { onclick } = $props();
</script>

<button {onclick}>Click me</button>

https://svelte-5-preview.vercel.app/docs/event-handlers#bubbling-events

So although I agree that using on:click forwarding in Svelte 3 & 4 was nice and preferable, It will make upgrading slightly harder.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request new rule
Projects
None yet
Development

No branches or pull requests

2 participants