Svelte Components Explained: Props & Composition Made Simple


Quick question: why do modern apps feel so smooth, even though they’re insanely complicated under the hood?

Open Amazon and peek at a product page. You’ll spot:

  • a shiny product gallery,
  • a reviews section full of opinions,
  • “you might also like” recommendations,
  • a payment form ready to take your money,
  • and of course, the login box that never remembers your password 🙃.

All of that looks like one seamless page, but behind the scenes each piece is its own little universe — often built by different teams, at different times, maybe even in different programming languages. Yet, somehow, it all just… works.

Same deal on Facebook: the news feed, stories, chat window, notifications — they’re all separate chunks, but together they create the illusion of one big, flowing app.

That’s the magic of components. Components let us break big, messy apps into smaller, reusable building blocks — like Lego. One block = one responsibility. Snap them together, and voilà: a complete app.

And Svelte makes this extra delightful, because a component is just a .svelte file. That’s it. No boilerplate. No ceremony. Just a file you import and drop into your app like it was born to be there.

But here’s where it gets even cooler 🤩: Svelte hands you a few little superpowers for handling data inside components:

  • $props() → mark the inputs your component gets from its parent,
  • $state() → keep track of local data that belongs to your component,
  • $derived() → compute new values automatically when other data changes.

Think of these as your toolkit for making components talk to each other, stay in sync, and do useful things without you manually wiring everything together.

In this guide, we’ll explore these helpers one by one, with tons of examples. By the end, you’ll be thinking in components like a pro — and maybe even having fun doing it. 🎉


Quick Setup Reminder 🛠️

As before, we’ll follow the usual SvelteKit project layout:

  • Put reusable components inside the src/lib folder.
  • Use those components inside your main page at src/routes/+page.svelte. (Yes it would be wiped cleean/replaced for each example)
  • Make sure you issued the npm run dev command in terminal for your project.

(If you landed here directly: this is the standard setup in SvelteKit. Think of lib as your toolbox of building blocks, and routes as the actual rooms in your house where you use those blocks.)




Section 1: Components Working Together

Now, let’s start simple. A single component is nice, but the real magic happens when you snap them together like Lego bricks.

Imagine a page with three parts: a header, a product list, and a footer. Each lives in its own file.

👉 Quick note: A .svelte file (a component) can optionally contain three sections:

  • a