We'll continue with our AddTodo and bindable Todo components from the state example.
When todos, a todo's completed property, or showCompleted change, visibleTodos will be updated. When the toggle is off, we only show unfinished todos.
Binding still updates the original todo, even when it's in a filtered list.
Bind a checkbox to the toggle state.
<script>
import Todo from './Todo.svelte';
import AddTodo from './AddTodo.svelte';
let todos = $state([]);
let showCompleted = $state(false);
let visibleTodos = $derived(
showCompleted
? todos
: todos.filter((todo) => !todo.completed)
);
function addTodo(todo) {
todos.push(todo);
}
</script>
{#each visibleTodos as todo (todo)}
<Todo
task={todo.task}
bind:completed={todo.completed}
/>
{/each}
<label>
<input type="checkbox" bind:checked={showCompleted}>
Show completed
</label>
<AddTodo onadd={addTodo} />