Explain it simply
Reusable Logic with Composables, in plain language
A composable is a function that packages up stateful logic using the Vue Composition API, and it's usually named starting with use. It should return state, computed values, and actions — no UI markup.
Problem → Why → Solution
The problem — why this concept matters
Calling a composable outside a component, in some random scope
A common mistake
Unwrapping a reactive value into a plain value and losing reactivity
Core concept — understand how it works
Extract Composition API logic into use... functions and reuse it across components.
- reactive source
- dependency tracking
- updated value or UI
Build a composable
Core syntax or pattern
// useCounter.js
import { ref, computed } from 'vue'
export function useCounter(initial = 0) {
const count = ref(initial)
const doubled = computed(() => count.value * 2)
const increment = () => count.value++
return { count, doubled, increment }
}
// component
const { count, doubled, increment } = useCounter(5)Extract Composition API logic into use... functions and reuse it across components.
Read the code step by step
- reactive source
// useCounter.jsA composable is a function that packages up stateful logic using the Vue Composition API, and it's usually named starting with use. It should return state, computed values, and actions — no UI markup.
- dependency tracking
const increment = () => count.value++Extract Composition API logic into use... functions and reuse it across components.
- updated value or UI
const { count, doubled, increment } = useCounter(5)Apply Reusable Logic with Composables in a small example and test normal, edge, and failure cases.
- reactive source
- dependency tracking
- updated value or UI
Success
Build a composable
Failure
Calling a composable outside a component, in some random scope
Let's break it down simply
A composable is a function that packages up stateful logic using the Vue Composition API, and it's usually named starting with use. It should return state, computed values, and actions — no UI markup.
// useCounter.js
import { ref, computed } from 'vue'
export function useCounter(initial = 0) {
const count = ref(initial)
const doubled = computed(() => count.value * 2)
const increment = () => count.value++
return { count, doubled, increment }
}
// component
const { count, doubled, increment } = useCounter(5)count = 5
doubled = 10Try it yourself
Write a useFetch composable that returns loading, data, error, and an execute function.
Composables — Vue.js
Real-world usage
How to apply it in a real project
Apply Reusable Logic with Composables in a small example and test normal, edge, and failure cases.
Mini challenge
Build it yourself
Write a useFetch composable that returns loading, data, error, and an execute function.
- Rebuild the example without copying it
- Test a normal input and an edge case
- Predict the compiler or output result before running it
// useCounter.js
import { ref, computed } from 'vue'
export function useCounter(initial = 0) {
const count = ref(initial)
const doubled = computed(() => count.value * 2)
const increment = () => count.value++
return { count, doubled, increment }
}
// component
const { count, doubled, increment } = useCounter(5)Expected: Build a composable
Show hint
Apply Reusable Logic with Composables in a small example and test normal, edge, and failure cases.
Quick quiz 1 / 3
Quick quiz 2 / 3
Quick quiz 3 / 3
What to remember
- reactive source → dependency tracking → updated value or UI
- Extract Composition API logic into use... functions and reuse it across components.
- Calling a composable outside a component, in some random scope
- Apply Reusable Logic with Composables in a small example and test normal, edge, and failure cases.
Composables — Vue.js