Component Events
Introduction
Svelte 5 introduces a new, simpler way to handle component events. This guide will walk you through the basics of this new approach, making it easy for beginners to understand and implement.
Basic Callback Props
Let's start with a simple example of how to use callback props in Svelte 5.
Now, let's see how to use this component in a parent component:
Let's see how this would appear in the browser:
[Button] Increment: 0
When you click the button, the count will increase, and you'll see log messages in the console.
Conclusion
This guide covers the bare essentials of handling component events in Svelte 5. The new approach using callback props simplifies event handling and makes your code more intuitive.
Remember, while createEventDispatcher
from previous Svelte versions still works in Svelte 5, it's recommended to use these new methods for future-proofing your code.
Last updated