Skip to main content

We can also spread event handlers directly onto elements. Here, we’ve defined an onclick handler in App.svelte — all we need to do is pass the props to the <button> in BigRedButton.svelte:

BigRedButton
<button {...props}>
	Push
</button>

Edit this page on GitHub

<script>
import BigRedButton from './BigRedButton.svelte';
import horn from './horn.mp3';

const audio = new Audio();
audio.src = horn;

function honk() {
audio.load();
audio.play();
}
</script>

<BigRedButton onclick={honk} />