svelte 5 counter component
- User
svelte 5 counter component
Assistant
I'll help you create a simple counter component in Svelte 5, which introduces some new features like runes. Here's a basic implementation:
<script> import { $state } from 'svelte'; let count = $state(0); function increment() { count++; } function decrement() { count--; } </script> <div class="counter"> <button on:click={decrement}>-</button> <span>{count}</span> <button on:click={increment}>+</button> </div> <style> .counter { display: flex; gap: 1rem; align-items: center; } button { padding: 0.5rem 1rem; font-size: 1.2rem; } span { font-size: 1.5rem; } </style>Key differences from Svelte 4:
- Uses the new
$staterune instead of the previous reactive declarations - No need for
$:reactive statements for this simple case - State management is more explicit with the rune system
Would you like me to explain any part of this code in more detail or show you how to add more features to the counter?
- Uses the new
Other models, same or similar prompt
Responses were produced in different, uncontrolled contexts (system prompts, settings, prior turns). This is not a benchmark.