Lesson 2 of 9 · 26m
Composables That Survive Real Features
How to design composables with clear ownership, lifecycle cleanup, typed inputs, and enough flexibility to avoid copy-paste forks.
A composable is not "a file with some refs in it." It is a stateful abstraction with an ownership boundary. The first time you write useMousePosition, the pattern feels trivial; the fifth production composable teaches you that cleanup, input normalization, and API shape are where these things succeed or fail.
The shape: accept flexible inputs, return stable objects
import { computed, unref, type MaybeRefOrGetter, toValue } from "vue";
export function useFilteredList<T>(
source: MaybeRefOrGetter<T[]>,
predicate: (item: T) => boolean,
) {
const items = computed(() => toValue(source).filter(predicate));
return { items };
}MaybeRefOrGetter inputs are one of Vue 3's most practical design tools. Callers can pass a ref, a plain value, or a getter. Inside, normalize with toValue instead of sprinkling unref guesses throughout the body. That one decision makes composables reusable in both setup code and other composables.
Ownership and cleanup are part of the API
export function useEventListener(
target: MaybeRefOrGetter<EventTarget | null | undefined>,
event: string,
listener: EventListener,
) {
watchEffect((onCleanup) => {
const el = toValue(target);
if (!el) return;
el.addEventListener(event, listener);
onCleanup(() => el.removeEventListener(event, listener));
});
}If a composable registers a listener, creates a timer, opens a socket, or observes a DOM node, it must clean up when the component unmounts or when inputs change. watchEffect's onCleanup is the mechanism. In production, "we leaked listeners after every route change" usually means this contract was ignored.
Don't return ten unrelated things
A good composable returns one coherent capability. useInvoice can expose the invoice, save state, validation errors, and mutation methods. It should not also own notifications, routing, permissions, and a second unrelated feature. If consumers routinely destructure only two of twelve fields, the abstraction is the wrong size.
Use composable factories sparingly
function createResourceStore<T>(key: string) {
return () => {
// instance-local state per caller
const data = shallowRef<T | null>(null);
return { data };
};
}Module-level state inside a composable is shared across every component that uses it. Sometimes that is correct — feature flags, connection status — but accidental sharing is a severe bug. If state must be per caller, define it inside the composable function, not at module scope.
Field notes
- Prefix composables with
use, but don't mistake the name for architecture — the ownership contract matters more. - Typed composables are documentation.
MaybeRefOrGetter, discriminated union results, and explicit return types save future maintainers. - Test composables as logic units with lifecycle behavior, not as implementation details.
- When a composable starts accepting five booleans to alter behavior, split it; configuration props have the same failure mode in logic as in components.
Was this lesson helpful?