Create a Reusable Vue Composable Function with Reactive State Management
Generate a fully typed Vue 3 composable with reactive state, computed properties, async methods, and lifecycle cleanup.
๐ The Prompt
Write a reusable Vue 3 composable function called `use[FEATURE_NAME]` that handles [DESCRIBE_FUNCTIONALITY] for a [APPLICATION_TYPE] application.
**Composable Specifications:**
1. **Reactive State**: Define the following reactive state variables:
- [STATE_VAR_1]: [TYPE] โ initialized to [DEFAULT_VALUE]
- [STATE_VAR_2]: [TYPE] โ initialized to [DEFAULT_VALUE]
- `isLoading`: boolean โ tracks async operation status
- `error`: string | null โ stores error messages
2. **Computed Properties**: Include these derived values:
- [COMPUTED_1]: Derives [DESCRIPTION] from the state
- [COMPUTED_2]: Returns a filtered/transformed version of [STATE_VAR_1]
3. **Methods**: Implement the following functions:
- `fetch[DATA]()`: Async function that calls [API_ENDPOINT] and updates state, with try/catch/finally pattern
- `update[DATA](payload)`: Optimistically updates local state before API confirmation
- `reset()`: Restores all state to initial values
4. **Lifecycle Integration**:
- Accept an options parameter `{ immediate: boolean, debounceMs: number }` to control auto-fetching behavior
- Clean up watchers and timers using `onUnmounted`
- Use `watchEffect` or `watch` to react to [DEPENDENCY] changes
5. **TypeScript Support**: Fully type the composable with:
- An interface for the options parameter
- An interface for the return type
- Generic type parameter for [DATA_ENTITY] if applicable
Provide the complete composable file, a usage example inside a `<script setup>` component, and brief inline documentation for each exported member.
๐ก Tips for Better Results
Specify whether you need Options API compatibility or script-setup only to get the right usage example. Mention your data-fetching library (Axios, fetch, ofetch) for consistent code. Request error retry logic or caching if your use case demands resilience.
๐ฏ Use Cases
Frontend developers use this when extracting complex stateful logic from Vue components into shareable, testable composable functions.
๐ Related Prompts
๐ป Coding
beginner
Explain Code Like Im a Beginner
Get any code explained in plain English with line-by-line breakdowns, analogies, and learning suggestions.
๐ป Coding
beginner
Debug My Code and Explain the Fix
Get your code debugged with clear explanations of what went wrong and why, plus the corrected version.
๐ป Coding
intermediate
Write Unit Tests for My Code
Generate thorough unit tests covering edge cases, error handling, and both positive and negative scenarios.
๐ป Coding
intermediate
Convert Code Between Languages
Convert code between any programming languages while maintaining idiomatic patterns and best practices.
๐ป Coding
intermediate
Write a REST API Endpoint
Generate production-ready REST API endpoints with validation, error handling, and documentation.
๐ป Coding
advanced
Create a GitHub Actions CI/CD Workflow for Automated Testing and Deployment
Generate a complete GitHub Actions CI/CD workflow with build, test, deploy, and notification jobs for your project.