Skip to content

Events

React invented synthetic events with camelCase handler props (onClick, onChange, onSubmit). Svelte 5 drops the special syntax entirely — event handlers are just lowercase HTML attributes, the same names the browser uses (onclick, oninput, onsubmit). If you know how to write an onclick attribute in a plain HTML file, you already know Svelte events.

React
function Counter() {
const [count, setCount] = React.useState(0);
return (
<button onClick={() => setCount(c => c + 1)}>
Count: {count}
</button>
);
}
Svelte
<script>
let count = $state(0);
</script>
<button onclick={() => count++}>
Count: {count}
</button>

Key points:

  • React: onClick (capital O, capital C). Svelte 5: onclick (all lowercase).
  • React needs a setter function from useState. Svelte 5 mutates $state directly — count++ just works.
  • In Svelte 5 the old on:click directive from Svelte 4 is gone. Always use lowercase onclick.

Inline arrow functions are fine for simple cases. Extract to a named function for readability:

React
function Form() {
const [value, setValue] = React.useState('');
function handleChange(e) {
setValue(e.target.value);
}
return <input value={value} onChange={handleChange} />;
}
Svelte
<script>
let value = $state('');
function handleInput(e) {
value = e.target.value;
}
</script>
<input {value} oninput={handleInput} />

Note: for two-way binding of inputs, bind:value is cleaner (covered in the Bindings lesson). The named handler pattern is shown here because it maps directly to the React onChange pattern.

You have full access to the native DOM event — no synthetic wrapper:

React
function KeyLogger() {
const [key, setKey] = React.useState('');
return (
<input
onKeyDown={e => setKey(e.key)}
placeholder="Press a key"
/>
);
}
Svelte
<script>
let key = $state('');
</script>
<input
onkeydown={e => (key = e.key)}
placeholder="Press a key"
/>
{#if key}
<p>Last key: <kbd>{key}</kbd></p>
{/if}
How do you attach a click handler in Svelte 5?
Svelte 4 used "on:click". What changed in Svelte 5?
You want to call e.preventDefault() on a form submit. How do you do it in Svelte 5?
What does Svelte 5 pass as the event argument to a handler?