Vue Composables Pattern
Extract and reuse stateful logic across components using Vue composables
When to Use
- Multiple components share the same stateful logic (e.g., window size tracking, API fetching, form validation)
- You want to extract complex logic out of a component's
<script setup> for readability and reuse
- Replacing Vue 2 mixins with a more explicit, composable approach
Instructions
- Create a function prefixed with
use (e.g., useWindowSize, useFetch).
- Inside, use
ref, reactive, computed, watch, and lifecycle hooks as needed.
- Return only what consumers need — keep internal state private.
- Co-locate composable files with their primary consumer or place in a
composables/ directory.
// composables/useWindowSize.ts
import { ref, onMounted, onUnmounted } from 'vue';
export function useWindowSize() {
const width = ref(window.innerWidth);
const height = ref(window.innerHeight);
function update() {
width.value = window.innerWidth;
height.value = window.innerHeight;
}
onMounted(() => window.addEventListener('resize', update));
onUnmounted(() => window.removeEventListener('resize', update));
return { width, height };
}
- Composables can accept parameters (refs or plain values) for configuration.
- Return cleanup functions or use lifecycle hooks to handle side-effect teardown.
Details
Composables are the Vue 3 Composition API's answer to code reuse. They replace mixins, which suffered from namespace collisions, implicit dependencies, and unclear data sources. A composable is a plain function that uses Vue's reactivity primitives, making dependencies explicit.
Trade-offs:
- Composables must be called during
setup() — they cannot be used conditionally or in loops (similar to React hooks rules)
- Deeply nested composable calls can make the dependency chain hard to trace
- Over-extracting into composables can fragment logic that is easier to understand inline
When NOT to use:
- For stateless utility functions — just export a plain function, no need for Vue reactivity
- When the logic is used by only one component — inline it in
<script setup> until reuse is needed
- For global state — use Pinia stores instead of a composable with module-level state
Source
https://patterns.dev/vue/composables-pattern
Process
- Read the instructions and examples in this document.
- Apply the patterns to your implementation, adapting to your specific context.
- Verify your implementation against the details and edge cases listed above.
Harness Integration
- Type: knowledge — this skill is a reference document, not a procedural workflow.
- No tools or state — consumed as context by other skills and agents.
Success Criteria
- The patterns described in this document are applied correctly in the implementation.
- Edge cases and anti-patterns listed in this document are avoided.
1---2name: vue-composables-pattern3description: Vue Composables Pattern4metadata:5 source: https://skillmd.com/skills/intense-visions/vue-composables-pattern6---7# Vue Composables Pattern89> Extract and reuse stateful logic across components using Vue composables1011## When to Use1213- Multiple components share the same stateful logic (e.g., window size tracking, API fetching, form validation)14- You want to extract complex logic out of a component's `<script setup>` for readability and reuse15- Replacing Vue 2 mixins with a more explicit, composable approach1617## Instructions18191. Create a function prefixed with `use` (e.g., `useWindowSize`, `useFetch`).202. Inside, use `ref`, `reactive`, `computed`, `watch`, and lifecycle hooks as needed.213. Return only what consumers need — keep internal state private.224. Co-locate composable files with their primary consumer or place in a `composables/` directory.2324```typescript25// composables/useWindowSize.ts26import { ref, onMounted, onUnmounted } from 'vue';2728export function useWindowSize() {29 const width = ref(window.innerWidth);30 const height = ref(window.innerHeight);3132 function update() {33 width.value = window.innerWidth;34 height.value = window.innerHeight;35 }3637 onMounted(() => window.addEventListener('resize', update));38 onUnmounted(() => window.removeEventListener('resize', update));3940 return { width, height };41}42```43445. Composables can accept parameters (refs or plain values) for configuration.456. Return cleanup functions or use lifecycle hooks to handle side-effect teardown.4647## Details4849Composables are the Vue 3 Composition API's answer to code reuse. They replace mixins, which suffered from namespace collisions, implicit dependencies, and unclear data sources. A composable is a plain function that uses Vue's reactivity primitives, making dependencies explicit.5051**Trade-offs:**5253- Composables must be called during `setup()` — they cannot be used conditionally or in loops (similar to React hooks rules)54- Deeply nested composable calls can make the dependency chain hard to trace55- Over-extracting into composables can fragment logic that is easier to understand inline5657**When NOT to use:**5859- For stateless utility functions — just export a plain function, no need for Vue reactivity60- When the logic is used by only one component — inline it in `<script setup>` until reuse is needed61- For global state — use Pinia stores instead of a composable with module-level state6263## Source6465https://patterns.dev/vue/composables-pattern6667## Process68691. Read the instructions and examples in this document.702. Apply the patterns to your implementation, adapting to your specific context.713. Verify your implementation against the details and edge cases listed above.7273## Harness Integration7475- **Type:** knowledge — this skill is a reference document, not a procedural workflow.76- **No tools or state** — consumed as context by other skills and agents.7778## Success Criteria7980- The patterns described in this document are applied correctly in the implementation.81- Edge cases and anti-patterns listed in this document are avoided.