Introduction
Components let you build small, reusable pieces of your app. They help keep your code neat and easy to manage.
Components let you build small, reusable pieces of your app. They help keep your code neat and easy to manage.
<script> // JavaScript logic here </script> <style> /* CSS styles here */ </style> <!-- HTML markup here --> <div> <!-- component content --> </div>
.svelte extension.<script> let name = 'Friend'; </script> <h1>Hello {name}!</h1>
<style>
h1 { color: blue; }
</style>
<h1>Blue Heading</h1><script> export let count = 0; </script> <button on:click={() => count++}> Clicked {count} times </button>
This component shows a styled heading with a customizable title. You can reuse it anywhere by changing the title.
<script> export let title = 'Welcome'; </script> <style> h2 { color: darkgreen; font-family: sans-serif; } </style> <h2>{title} to my Svelte app!</h2>
Components help you avoid repeating code by reusing the same piece multiple times.
Each component manages its own style and logic, so changes stay local and safe.
Think of components like LEGO blocks that you snap together to build your app.
Components break your app into small, manageable parts.
They make your code reusable and easier to update.
Svelte components combine HTML, CSS, and JavaScript in one file.
<script>
export let name = 'Friend';
</script>
<h1>Hello {name}!</h1><Greeting name="Anna" />?name with default 'Friend'. Passing name="Anna" overrides this default.{name} in the template will show 'Anna' because the prop was passed.<script>
let count = 0
function increment() {
count++
}
</script>
<button on:click="increment()">Click</button>
<p>Count: {count}</p>on:event={handler} without quotes around the handler.on:click="increment()" with quotes, which is incorrect and causes a syntax error.Pass the test and this pattern moves unknown