Introducing State Management with Runes
Introducing State Management with Runes
Welcome to the second course in our "Building A Kanban Board" series! In our previous course, we built the foundational UI components for our Kanban board: the main layout, columns, and task cards.
Now it’s time to make our board fully functional by connecting it to dynamic data using Svelte's Runes API.
Bridging from UI to State
While our components from the previous course look great, they're currently displaying static, hardcoded tasks. In a real Kanban application, we need:
- A central place to store all our tasks
- A way to filter tasks by status (To Do, In Progress, Done)
- The ability to move tasks between columns
- A reactive system that updates the UI when tasks change
That’s where state management comes in — and Svelte’s Runes provide an elegant solution.
What is State Management
State management is a fundamental concept in modern web applications. It refers to how we organize, store, and update data that changes over time in our application. For our Kanban board, we'll need to manage tasks that can move between different columns (To Do, In Progress, and Done), and we'll need our UI to update automatically when this data changes.
Svelte 5 introduces a new way to handle reactivity called Runes. Runes are special functions prefixed with a dollar sign ($) that enhance variables with special behaviors. They replace the older reactive syntax in previous Svelte versions and provide a more intuitive way to work with reactive state.
In this lesson, we'll focus on creating a task store that will serve as the central data repository for our Kanban board application. This store will:
- Hold our collection of tasks.
- Provide filtered lists of tasks for each column.
- Expose functions to access these lists from our components.
- Connect this accessor functions to our board component
Let's get started by exploring how to create reactive state with Svelte 5's Runes!
Creating Reactive State with `$state`
The first step in building our task store is to create a reactive state variable that will hold our collection of tasks. For that we use the $state rune to make a reactive variable.
Here's how we'll create our initial tasks array:
Let's break down what's happening here:
- We're creating a file called
taskStore.svelte.jsin thesrc/libdirectory. - We're using the
$staterune to make ourtasksarray reactive. - We're initializing it with four sample tasks.
- Each task has an
id,title,description, andstatusproperty. - The
statusproperty determines which column the task belongs to.
When we use $state, Svelte automatically tracks any changes to this variable. If we add, remove, or modify tasks in this array, any components that depend on this data will automatically update.
For example, if we were to add a new task:
Svelte would detect this change and update any UI elements that display our tasks. This is the power of reactivity in Svelte — we don't need to manually trigger UI updates when our data changes.
