Build reusable composables for Vue 3
✓Works with OpenClaudeYou are a Vue 3 expert. The user wants to build reusable composables that encapsulate logic, state, and lifecycle hooks for Vue 3 components.
What to check first
- Verify Vue 3 is installed with
npm list vue(should be 3.x.x) - Check that
<script setup>syntax is available in yourvite.config.jsor build config - Confirm you're using the Composition API, not Options API
Steps
- Import
ref,computed,watch, oronMountedfromvueat the top of your composable file - Create a function with a descriptive name starting with
use(e.g.,useFetch,useCounter) - Define reactive state inside the function using
ref()orreactive() - Add watchers with
watch()orwatchEffect()if side effects are needed - Implement lifecycle hooks like
onMounted()oronUnmounted()directly in the composable - Return an object containing only the state and methods that components should access
- Import the composable into a component and call it at the top level of
<script setup> - Access returned properties and methods directly in the template
Code
// useCounter.js — a reusable counter composable
import { ref, computed, watch } from 'vue'
export function useCounter(initialValue = 0, maxValue = 100) {
// Reactive state
const count = ref(initialValue)
const history = ref([initialValue])
// Computed properties
const isAtMax = computed(() => count.value >= maxValue)
const percentProgress = computed(() => (count.value / maxValue) * 100)
// Methods
const increment = () => {
if (!isAtMax.value) {
count.value++
}
}
const decrement = () => {
if (count.value > 0) {
count.value--
}
}
const reset = () => {
count.value = initialValue
history.value = [initialValue]
}
// Watch for changes and track history
watch(count, (newVal) => {
history.value.push(newVal)
})
// Return public interface
return {
count,
isAtMax,
percentProgress,
history,
increment,
decrement,
reset
}
}
// Usage in a component:
// <script setup>
// import { useCounter } from './useCounter.js'
// const { count, isAtMax, increment, decrement, reset } = useCounter(0, 50)
// </script>
//
// <template>
// <div>
// <p>Count: {{ count }}</p>
// <p v-if="isAtMax">Max reached!</p>
//
Note: this example was truncated in the source. See the GitHub repo for the latest full version.
Common Pitfalls
- Treating this skill as a one-shot solution — most workflows need iteration and verification
- Skipping the verification steps — you don't know it worked until you measure
- Applying this skill without understanding the underlying problem — read the related docs first
When NOT to Use This Skill
- When a simpler manual approach would take less than 10 minutes
- On critical production systems without testing in staging first
- When you don't have permission or authorization to make these changes
How to Verify It Worked
- Run the verification steps documented above
- Compare the output against your expected baseline
- Check logs for any warnings or errors — silent failures are the worst kind
Production Considerations
- Test in staging before deploying to production
- Have a rollback plan — every change should be reversible
- Monitor the affected systems for at least 24 hours after the change
Related Vue.js Skills
Other Claude Code skills in the same category — free to download.
Vue Component
Create Vue 3 components with Composition API and props
Vue Pinia
Set up Pinia stores for state management in Vue
Vue Router
Configure Vue Router with navigation guards and dynamic routes
Vue Nuxt
Scaffold Nuxt 3 app with SSR and auto-imports
Vue Testing
Write Vue component tests with Vitest and Testing Library
Vue TypeScript
Set up Vue 3 with TypeScript and type-safe props
Vue 3 Composables Pattern
Build reusable composables that share logic across Vue 3 components
Vue Pinia Global State
Manage global app state in Vue 3 with Pinia (modern Vuex replacement)
Want a Vue.js skill personalized to YOUR project?
This is a generic skill that works for everyone. Our AI can generate one tailored to your exact tech stack, naming conventions, folder structure, and coding patterns — with 3x more detail.