State Management and Data Persistence
The previous chapter completed componentization; now we face the last hands-on pain point:Page refresh, data lost.
To solve this, we will introduce Vue's officially recommended state management library Pinia, and combine it with Tailwind v4 's styling capability to achieve datapersistent storage.
Pinia State Management and Data Persistence
Pinia is a lightweight state management library for Vue.js that lets you share and manage state between components. We can think of Pinia as a global data warehouse from which all components can fetch or update data.
Pinia related references:Pinia Getting Started Tutorial.
Why use Pinia?
In the previous code, the data was all in App.vue . If the project grows (e.g., adding a statistics page, user center), passing data between components becomes a nesting nightmare (Props Hell).
- Pinia provides a global data warehouse.
- Any component can directly fetch data from the warehouse or trigger the warehouse's methods.
Installation and initialization:
npm install pinia
npm install @vue/devtools-kit -D
Register in src/main.js:
Example
import
{
createApp
}
from
'vue'
import
{
createPinia
}
from
'pinia'
import
App from
'./App.vue'
import
'./style.css'
const
app
=
createApp
(
App
)
app.
use
(
createPinia
(
)
)
// plugin registration
app.
mount
(
'#app'
)
Create Task Store (warehouse)
Create a stores directory under src, then create taskStore.js under src/stores.

We will move all the logic originally in App.vue over here.
Example
import
{
defineStore
}
from
'pinia'
import
{
ref
,
computed
,
watch
}
from
'vue'
export
const
useTaskStore
=
defineStore
(
'task-store'
,
(
)
=>
{
// --- 1. State ---
// try to read the initial value from LocalStorage
const
savedTasks
=
localStorage.
getItem
(
'my-tasks'
)
const
tasks
=
ref
(
savedTasks
?
JSON.
parse
(
savedTasks
)
:
[
]
)
const
filter
=
ref
(
'all'
)
// --- 2. Derived State (Getters) ---
const
filteredTasks
=
computed
(
(
)
=>
{
if
(
filter.
value
===
'active'
)
return
tasks.
value
.
filter
(
t
=>
!
t.
isCompleted
)
if
(
filter.
value
===
'completed'
)
return
tasks.
value
.
filter
(
t
=>
t.
isCompleted
)
return
tasks.
value
}
)
// --- 3. Actions ---
const
addTask
=
(
title
)
=>
{
tasks.
value
.
unshift
(
{
id
:
crypto.
randomUUID
(
)
,
title
,
isCompleted
:
false
}
)
}
const
removeTask
=
(
id
)
=>
{
tasks.
value
=
tasks.
value
.
filter
(
t
=>
t.
id
!==
id
)
}
const
toggleTask
=
(
id
)
=>
{
const
task
=
tasks.
value
.
find
(
t
=>
t.
id
===
id
)
if
(
task
)
task.
isCompleted
=
!
task.
isCompleted
}
// --- 4. Persistence ---
// watch tasks for changes, and write to LocalStorage once changed
watch
(
tasks
,
(
newVal
)
=>
{
localStorage.
setItem
(
'my-tasks'
,
JSON.
stringify
(
newVal
)
)
}
,
{
deep
:
true
}
)
// deeply watch changes to objects inside the array
return
{
tasks
,
filter
,
filteredTasks
,
addTask
,
removeTask
,
toggleTask
}
}
)
Refactor App.vue (connect to the warehouse)
The current App.vue no longer needs to manage data itself; it just calls the Store.
Example
v-for="task in filteredTasks" :key="task.id" :task="task" @toggle="toggleTask" @remove="removeTask" />
Detailed Knowledge Points
1. storeToRefs What is it?
If you write it directly like this:const { filter } = taskStore, you will lose reactivity. When you modify filter , the page will not refresh.
- Reason: The Store is an object wrapped by
reactive; direct destructuring breaks the reference. - Solution: When using
storeToRefs. It converts the state in the warehouse intoref, ensuring that after destructuring it can still "move the whole body with a single hair".
2. watch 's deep watching (deep: true)
In the persistence logic, we are watching the tasks array.
- If
{ deep: true }is not added, the save is triggered only when the array is replaced (e.g.,tasks.value = []). - After adding it, modifications to properties of objects inside the array (such as a task changing from "To Do" to "Done") are also captured and stored on disk.
3. Persistence approach
We adopted a closed loop of "read on init + write on change":
- Read: The Store reads from
localStorageon creation to give the application "memory". - Write: Uses
watchto achieve fully automated synchronization; developers no longer need to manually writesetItem.
Tailwind v4 Final Polish
With the support of the Store, we can add someglobal feedback styles. For example, in TaskHeader.vue display task progress:
Example
TaskHub
Completion progress
{{ progress }}%
:style="{ width: ${progress}% }"
>
After modification, the entire interface gains a progress bar feature:
