init to prod
Week 2 - Components
Thursday, October 15, 2026
This week: components
- Split last week’s todo list into smaller, reusable pieces.
- Pass data from a parent component to its children.
- Connect buttons in child components to actions.
- Briefly compare Svelte components with native web components.
Why split up an interface?
- Give each piece a clear responsibility.
- Reuse the same markup for more than one list or task.
- Make changes in one place instead of repeating code.
- Keep each component easier to read and test.
The todo component tree
App
|-- Todolist
| |-- Todo
| `-- Todo
`-- Todolist
`-- Todo
The week 2 example has two lists: each receives its own array of tasks.
Passing data with props
In the parent, import the component and pass it a value:
<script>
import Todolist from './Todolist.svelte';
let todos = $state(["Go Shopping", "Read a book"]);
</script>
<Todolist todos={todos} />
In Todolist.svelte, receive it with let { todos } = $props();.
From list to task
Todolist.svelte can render a Todo for each item:
{#each todos as task, i}
<Todo task={task} i={i}
deletefunction={() => removeTodo(i)} />
{/each}
The task and index are data props; deletefunction is a callback prop that lets the button trigger removal.
A reusable task component
<script>
let { task, i, deletefunction } = $props();
</script>
<li class="w3-card w3-padding w3-green">
Task {i + 1} - {task}
<button onclick={deletefunction}>Remove</button>
</li>
The same component can display any task passed to it.
What about vanilla JavaScript?
- Native web components let browsers use custom HTML elements.
- Define a class extending
HTMLElement and register it with customElements.define(...).
- Svelte components are authored in
.svelte files and use Svelte’s props and reactive state.
- Both approaches help package reusable pieces of an interface.
Thank you! Any questions?