Write a basic svelte 5 counter component
- User
Write a basic svelte 5 counter component
Assistant
Svelte 5 Counter Component
Here's a basic counter component in Svelte 5 using the new Runes syntax:
<script> let count = $state(0); function increment() { count += 1; } function decrement() { count -= 1; } function reset() { count = 0; } </script> <div class="counter"> <h1>Counter: {count}</h1> <div class="buttons"> <button on:click={decrement}>-</button> <button on:click={reset}>Reset</button> <button on:click={increment}>+</button> </div> </div> <style> .counter { text-align: center; font-family: Arial, sans-serif; max-width: 300px; margin: 0 auto; padding: 20px; border: 1px solid #ddd; border-radius: 8px; } h1 { color: #333; } .buttons { display: flex; gap: 10px; justify-content: center; } button { padding: 8px 16px; font-size: 16px; cursor: pointer; background-color: #4CAF50; color: white; border: none; border-radius: 4px; } button:hover { background-color: #45a049; } </style>Key Features:
- Uses Svelte 5's
$staterune to create reactive state - Includes increment, decrement, and reset functionality
- Basic styling for a clean look
- Simple, component-scoped CSS
- Follows Svelte's component structure (script, markup, style)
You can use this component in your Svelte 5 application by importing it where needed. The counter state is fully reactive and will update the UI automatically when changed.
Note: This assumes you're using Svelte 5 with Runes enabled. If you're using an older version of Svelte, you would need to adapt the state management syntax.
- Uses Svelte 5's
Other models, same or similar prompt
Responses were produced in different, uncontrolled contexts (system prompts, settings, prior turns). This is not a benchmark.