Documentation Shento Hendriks
Svelte / SvelteKit
Svelte / SvelteKit
  • Getting Started with SvelteKit
    • Installing SvelteKit
      • Tailwind Typography plugin
    • Base Syntax & Core Features of Svelte
      • Curly Braces & Understanding Core Syntax
      • Reactive Variables
      • Two-way-binding Shortcut
      • Using multiple components
      • Components & Communication via Props
      • Display HTML in Svelte
      • Dynamic classes
      • Using $effect
      • Working with Javascript and Rune reactivity
    • Working with Conditionals & Loops
      • Showing code conditionally in Svelte
      • Looping through arrays (lists)
      • Lists & Keys
    • Closer look at reactivity
      • Updating Arrays & Objects
      • Event Modifiers
    • Component Events
    • Best Practices
    • Adding Google Font in Tailwind and SvelteKit
    • Adding Local Fonts to SvelteKit with Tailwind
    • Prevent flicker in images
    • Enhanced Images
    • Form Submissions in SvelteKit
    • Effective SEO in SvelteKit
    • Automatic Site Maps in Svelte
    • Creating Custom Elements with Svelte, SvelteKit, and Vite
    • Creating a Svelte Library with JavaScript Functions
  • Useful Code Snippets
    • Hover effects
    • Navigation
    • Centering Items
    • Setting Footer Bottom Page
  • Installing Useful Libraries
    • Documenting Javascript with JSDocs (Crash Course)
    • Adding Lottie Animations to Svelte Application
    • SvelteKit Melt-ui
      • First Component - accordion
    • Installing Shadcn/ui Svelte
    • Getting Started with GSAP with SvelteKit
      • What is GSAP?
      • Installing GSAP & Your first animation
      • GSAP in SvelteKit
    • Installing Locomotive Scroll with Svelte 5 and SvelteKit
  • SvelteKit Server Fetching and Deployment
    • Preparing a SvelteKit application for Deployment
    • Utility Functions for URL Handling in SvelteKit
    • Fetching Directus Data from Rest API with SvelteKit
    • Fetching data from api with caching
    • Displaying Fetched Data in a Each Loop
    • Creating slug friendly urls based on query data
    • Finding correct query in api based on url slug
Powered by GitBook
On this page
  • Introduction
  • Basic Callback Props
  • Conclusion
  1. Getting Started with SvelteKit

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.

<script>
  let { onIncrement } = $props();
  let count = $state(0);

  function increment() {
    count++;
    onIncrement(count);
  }
</script>

<button onclick={increment}>Increment: {count}</button>
  • $props() is a Svelte 5-specific feature that declares props for the component.

  • onIncrement is a prop name we've chosen, but you can use any valid JavaScript identifier.

  • on:click is Svelte-specific syntax for adding event listeners.

Now, let's see how to use this component in a parent component:

<script>
  import Counter from './components/Counter.svelte';

  function handleIncrement(newCount) {
    console.log('New count:', newCount);
  }
</script>

<Counter onIncrement={handleIncrement} />
  • import is standard JavaScript syntax for importing modules.

  • onIncrement={handleIncrement} is passing a function as a prop, which is standard JavaScript/JSX syntax.

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.

PreviousEvent ModifiersNextBest Practices

Last updated 9 months ago