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.
javascript
// 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)You should see
count = 5
doubled = 10Try it yourself
Write a useFetch composable that returns loading, data, error, and an execute function.
Composables — Vue.js