♻️
Extract Shared Hook from Components
Identify duplicated logic across components and extract into a reusable hook
prompt template
Analyze these components for duplicated logic and extract shared hooks: ``` [PASTE_COMPONENT_1] ``` ``` [PASTE_COMPONENT_2] ``` For each identified pattern: 1. **Duplicated logic** — highlight the repeated code with line numbers 2. **Custom hook** — extract into a named hook with TypeScript generics 3. **Parameters** — what the hook accepts for customization 4. **Return value** — typed return object/tuple 5. **Refactored components** — show both components using the new hook 6. **Tests** — unit tests for the extracted hook using renderHook Rules: - Only extract if the pattern appears 2+ times - Keep hooks focused — one responsibility per hook - Preserve exact existing behavior — no functional changes - Use TypeScript generics where the types vary between uses
How to Use This Prompt
- 1Copy the prompt template above
- 2Paste into Claude, ChatGPT, or Cursor
- 3Replace [bracketed placeholders] with your specific project details
- 4Iterate on the AI output to refine and customize the results
More Refactoring Prompts
Related refactoring templates from the vault
♻️
Switch Statement to Strategy Pattern
Use this prompt to switch statement to strategy pattern with AI assistance
Beginnerrefactoringstrategy-pattern
♻️
Code Review Prompt
Get a thorough code review with actionable feedback
Beginnercode-reviewquality
♻️
Code Smell Detector and Fixer
Identify code smells in existing code and get refactored versions
Intermediaterefactoringcode-review
♻️
Nested Conditionals Flattener
Use this prompt to nested conditionals flattener with AI assistance
Intermediaterefactoringconditionals
You Might Also Like
Prompts from other categories that share similar tags
🎨
Frontend
Accessible Form with Validation
Build a form with proper accessibility, validation, and error handling
Shared:react
📝
Documentation
Code Documentation with JSDoc
Add comprehensive JSDoc documentation to existing code
Shared:typescript
⚡
Performance
React Rendering Performance Fix
Fix unnecessary re-renders and optimize React component performance
Shared:react
🎨
Frontend
React Component from Sketch Description
Turn a natural-language UI description into a polished React component
Shared:react
Keep Exploring the Vault