react
Refactor a messy React component into clean, reusable pieces
Guides an AI to safely refactor a large, tangled React component into smaller composable pieces while preserving exact behavior.
Who should use this
Is this prompt for you?
- Developers with a component that has grown past 200-300 lines and mixes concerns
- Teams inheriting AI-generated or legacy React code that needs cleanup
- Anyone about to add a feature to a component that's already hard to reason about
The prompt
Copy this prompt
Prompt
Refactor the following React component. Do not change its visible behavior — this is a structural refactor, not a feature change. [paste the full component code here] Tech context: React [version], TypeScript, [state management, e.g. useState/useContext/Zustand/Redux], styling with [Tailwind/CSS modules/styled-components]. Goals: 1. Split the component into smaller, single-responsibility pieces (presentational vs. logic, extracted subcomponents where it improves readability) 2. Extract repeated logic into custom hooks where it makes sense 3. Add or fix TypeScript types — no `any`, and props should have explicit interfaces 4. Remove dead code, unused props, and unnecessary re-renders (e.g. inline function definitions causing child re-renders, missing memoization where it actually matters) 5. Keep all existing behavior identical — same props in, same rendered output and interactions out For each change, briefly explain WHY you made it, not just what changed. After refactoring: - List every new file/component you created and where it lives - Point out anything you noticed that looks like a pre-existing bug, without fixing it unless I ask - Suggest tests I should add to lock in the current behavior before this refactor ships
Works well with Claude Code, Cursor and GitHub Copilot.
How to use it
Getting the best result
- 1Paste the complete original component, including its imports, so the AI has full context
- 2Specify your exact React version and state management approach for accurate suggestions
- 3Diff the refactored output against the original behavior manually or with existing tests before merging
- 4Ask it to fix any "noticed but not fixed" bugs in a separate follow-up prompt, not mixed into the refactor
Expected result
What you should get back
- The component split into smaller, well-typed pieces with a clear responsibility each
- Custom hooks extracted for reusable logic
- A written explanation of each structural change and its rationale
- A list of suggested tests to protect the refactor going forward
Tips
Get more out of this prompt
- Refactor and feature work should be separate prompts and separate commits — never mix them
- If the component has no tests yet, ask for a small snapshot or behavior test first, then refactor
- Explicitly forbid behavior changes in the prompt; AI tools will otherwise "improve" logic you didn't ask to change
Common mistakes
What to watch out for
- Letting the AI change behavior and structure in the same pass, making it hard to verify nothing broke
- Not pasting the full original file, leading to incomplete or inconsistent refactors
- Skipping a manual review of prop names and types after the split — renamed props can silently break parent components
Related prompts
Keep going
debugging
Debug a React state bug with a systematic root-cause prompt
Forces a systematic root-cause investigation of a React state or rendering bug instead of a surface-level patch that might mask the real issue.
Copy prompttestingGenerate a thorough unit test suite for existing code
Generates a unit test suite that actually covers edge cases and failure paths for existing code, instead of only testing the happy path.
Copy promptuiDesign and build an accessible, reusable UI component
Gets an AI to design a UI component's full set of states and variants up front, then implement it accessibly, instead of building only the happy-path visual state.
Copy promptReady to put this prompt to work?
Pair it with an AI coding tool and a real project, then ship something this week.