0Pricing
Vue Academy · Lesson

What Makes a Good Composable

Naming convention (useName), encapsulating reactive state, returning refs not raw values.

What Makes a Good Composable is a free Vue Academy lesson on CoddyKit — lesson 1 of 4. You can read the complete lesson below for free — then practise it hands-on in the browser with a built-in code editor and a 24/7 AI tutor. It is part of the Vue Academy learning path, one of 4 lessons in the course, and your progress syncs across the web and the CoddyKit app.

What Is a Composable

A composable is a function that uses Vue Composition API to encapsulate and reuse stateful logic. It is the Vue 3 answer to mixins - cleaner, explicit, and type-friendly.

The use Prefix Convention

By convention composables are named with a use prefix: useCounter, useFetch, useMouse. This instantly signals that the function holds reactive logic.

function useDarkMode() { /* ... */ }
function usePagination() { /* ... */ }

Return Refs, Not Raw Values

Return refs (or reactive state) so consumers stay reactive when they destructure. Returning count.value would hand back a snapshot that never updates.

function useCounter() {
  const count = ref(0);
  return { count }; // return the ref itself
}

Safe Destructuring

Because you return refs, the consumer can destructure freely without losing reactivity. Each ref keeps its own reactive connection.

const { count } = useCounter();
// count stays reactive after destructuring

Single Responsibility

A good composable does one thing. useFetch fetches; useLocalStorage persists. Small focused composables compose into bigger features cleanly.

Composing Composables

Composables can call other composables, building layered logic. A useUserProfile might use useFetch internally.

function useUserProfile(id) {
  const { data } = useFetch("/users/" + id);
  return { user: data };
}

Clean Up Side Effects

If a composable adds listeners, timers, or subscriptions, it must remove them in onUnmounted to prevent memory leaks.

import { onMounted, onUnmounted } from "vue";
onMounted(() => window.addEventListener("resize", onResize));
onUnmounted(() => window.removeEventListener("resize", onResize));

Accept Reactive Arguments

Composables can accept refs or getters as inputs and react to them with watch or watchEffect, making them flexible.

function useDouble(source) {
  return computed(() => unref(source) * 2);
}

Avoid Naming Conflicts

When a composable returns many values, expose clear names. If two composables return data, rename on destructure to avoid clashes.

const { data: user } = useFetch("/user");
const { data: posts } = useFetch("/posts");

Keep It Framework-Idiomatic

Call composables synchronously at the top level of setup (or another composable), not inside conditionals or async callbacks, so lifecycle hooks register correctly.

// good: top-level
const { count } = useCounter();
// avoid: inside if-blocks or after await

Documentation and Return Shape

Return a predictable object so consumers know what they get. A consistent shape (state plus methods) makes composables easy to learn and use.

return { count, increment, decrement, reset };

Quick Check

Why should a composable return refs instead of raw values?

Recap

You learned the marks of a good composable: the use prefix, returning refs for safe destructuring, single responsibility, cleaning up side effects in onUnmounted, and avoiding naming conflicts on the returned object.

Frequently asked questions

Is the “What Makes a Good Composable” lesson free?

Yes — the full text of “What Makes a Good Composable” is free to read here on the web, and the Vue Academy course includes 4 lessons in total. To practise it interactively (a built-in code editor and a 24/7 AI tutor) and unlock the rest of the Vue Academy course, upgrade to CoddyKit PRO.

What will I learn in “What Makes a Good Composable”?

Naming convention (useName), encapsulating reactive state, returning refs not raw values. You practise Vue Academy with hands-on code you run directly in the browser, and a 24/7 AI tutor answers your questions as you work through the lesson.

Do I need any experience to start Vue Academy?

No prior experience is required. Vue Academy on CoddyKit is structured for beginners through advanced learners; this is — lesson 1 of 4, so you can start here or from the beginning and move at your own pace.

How long does the “What Makes a Good Composable” lesson take?

Most CoddyKit lessons take about 5–10 minutes. Each one is bite-sized and interactive, so you make steady progress and pick up exactly where you left off across the web and the app.

Can I write and run code in this Vue Academy lesson?

Yes. Every Vue Academy lesson includes a built-in code editor, so you write and run real code right in your browser and get instant AI feedback — no local setup required.

All lessons in this course

  1. What Makes a Good Composable
  2. useCounter: A Simple Composable
  3. useFetch: Async Data Composable
  4. VueUse: The Composable Library
← Back to Vue Academy