Week 2 - Components

Date

Thursday, October 15, 2026

This week we’ll be understanding how to create components in Svelte, we will also have a brief look at this process in vanilla js with webcomponents.

Code Examples

View this example in the svelte playground here - Svelte Playground: Week 2 - Components

<script>
    import Todolist from './Todolist.svelte'
    
    let todos = $state(["Go Shopping", "Read a book", "Make some money", "Eat some chips"]);
    let textvalue = $state("");
    function addTodo(){
        todos.push(textvalue);
        textvalue = "";
    }

    let todos2 = $state(["Sleep", "Rest", "Procrastinate"]);
    let textvalue2 = $state("");
    function addTodo2(){
        todos2.push(textvalue2);
        textvalue = "";
    }
</script>

<link rel="stylesheet" href="https://www.w3schools.com/w3css/6/w3.css">

<div class="w3-container w3-padding">
    <h1>Header ({todos.length} tasks)</h1>
    <div class="w3-container w3-padding">
        <input bind:value={textvalue}>
        <button onclick={addTodo}>Add Task</button>
    </div>
    
    <Todolist todos={todos} />
</div>

<div class="w3-container w3-padding">
    <h1>Todolist 2 ({todos2.length} tasks)</h1>
    <div class="w3-container w3-padding">
        <input bind:value={textvalue2}>
        <button onclick={addTodo2}>Add Task</button>
    </div>
    
    <Todolist todos={todos2} />
</div>
<script>
    import Todo from './Todo.svelte'
    
    let {todos} = $props();

    function removeTodo(i){
        todos.splice(i,1);
    }
</script>

<ul>
        {#each todos as t, i}
            <Todo task = {t} i = {i} deletefunction={()=> removeTodo(i)}/>
        {/each}
</ul>
<script>
    let {task, i, deletefunction} = $props();
    
</script>

<link rel="stylesheet" href="https://www.w3schools.com/w3css/6/w3.css">

<li class="w3-card w3-padding w3-green">Task {i+1} - {task} <button class="w3-right w3-blue" onclick={deletefunction}>X</button></li>

Webinar Recording

Slides

link to slides