Logic Reuse and Performance Optimization
After completing routing and login logic, as the project grows, we find some logic (such as accessing local data and popping up notifications) repeats across multiple pages. At the same time, if the task list reaches thousands of items, the page may lag.
In this phase, we will use the in-depth application of the Composition API to solve code reuse and performance bottleneck problems.
Logic reuse: Custom Composables (Hooks)
Composables are the essence of Vue 3; they let us extract logic (State + Actions) into independent functions.
useLocalStorage: Extracting data persistence logic
No longer hand-write localStorage.getItem.
Example
// src/composables/useLocalStorage.js
import
{
ref
,
watch
}
from
'vue'
;
export
function
useLocalStorage
(
key
,
defaultValue
=
null
)
{
// 1. initialize data
const
storedValue
=
localStorage.
getItem
(
key
)
;
const
data
=
ref
(
storedValue
?
JSON.
parse
(
storedValue
)
:
defaultValue
)
;
// 2. watch for changes and auto-sync
watch
(
data
,
(
newValue
)
=>
{
localStorage.
setItem
(
key
,
JSON.
stringify
(
newValue
)
)
;
}
,
{
deep
:
true
}
)
;
return
data
;
}
useNotification: Extracting common UI logic
Implements a simple notification feature.
Example
// src/composables/useNotification.js
import
{
ref
}
from
'vue'
;
export
function
useNotification
(
)
{
const
message
=
ref
(
''
)
;
const
isVisible
=
ref
(
false
)
;
const
notify
=
(
msg
,
duration
=
2000
)
=>
{
message.
value
=
msg
;
isVisible.
value
=
true
;
setTimeout
(
(
)
=>
{
isVisible.
value
=
false
;
}
,
duration
)
;
}
;
return
{
message
,
isVisible
,
notify
}
;
}
Performance tuning: handling large data volumes and high-frequency rendering
When your TaskHub grows from managing 10 tasks to 1000 tasks, Vue's default "deep reactivity" brings computational pressure.
shallowRef The difference between markRaw
Vue's default ref is recursively reactive (i.e., every property inside the object is proxied).
shallowRef: only listens to .value 's reference change, not changes to the object's internal properties.
- Scenario: when you fetch a large batch of read-only task history records from the backend.
markRaw: marks an object so that itneveris converted to reactive.
- Scenario: Some complex third-party library instances (such as ECharts chart instances, map instances) would error or consume extreme performance if wrapped as reactive.
// 性能优化示例
import { shallowRef, markRaw } from 'vue';
// Assume this is ten thousand historical archive data records
const archiveTasks = shallowRef([]);
const loadArchive = (data) => {
// Triggers a reactive update only upon assignment, not upon internal property modification
archiveTasks.value = data;
};
v-once The difference between v-memo (directive-level optimization)
v-once: static content is rendered only once
If some task content never changes after rendering (such as the task's creation time), use v-once.
创建于: {{ task.createdAt }}
- Principle: After Vue renders it, it skips subsequent update checks for that node.
v-memo: update on demand (Vue 3.2+)
This is the most powerful directive for optimizing long lists. It accepts a dependency array; only when a value in the array changes will the node and its children re-render.
{{ task.title }} - {{ task.isCompleted }}
When should you tune?
| Solution | Problem Solved | Recommendation scenarios |
|---|---|---|
| Composables | Code duplication, scattered logic | Share logic across components (such as login checks, theme switching) |
| shallowRef | Memory overhead from deep proxying of large objects | List data size > 1000 and only whole replacement is needed |
| v-memo | Frequent long-list virtual DOM diffing | Complex v-for lists where only a few properties of each item change |
Integrating into the previous project
You can try TaskItem.vue to apply v-memo:
...
Now your project not only has clear logic (Composables) but also strong performance.