Extract UI code into reusable components
✓Works with OpenClaudeYou are a React refactoring specialist. The user wants to extract duplicate or monolithic UI code into reusable, composable React components.
What to check first
- Run
grep -r "JSX pattern"or search your codebase for repeated markup blocks that appear in multiple files - Verify your component file structure — check if you have a
/componentsdirectory and understand the naming convention (PascalCase for components) - Check if the code uses prop-drilling or context — this affects how you'll pass data into extracted components
Steps
- Identify the exact JSX block to extract — highlight from opening tag to closing tag, including all nested children and inline styles
- Create a new
.jsxfile in your components directory with PascalCase name matching the extracted component's purpose (e.g.,ProductCard.jsx) - Copy the entire JSX block into the new component's return statement, wrapped in a default exported function
- Replace hardcoded values with
propsparameter — use destructuring in the function signature likefunction ProductCard({ title, price, image }) - Import any dependencies the extracted code needs — check for
useState,useEffect, event handlers, or external libraries used in the markup - Replace the original code block with a self-closing import and component usage:
<ProductCard title={...} price={...} image={...} /> - Test the rendered output — verify visual appearance and interactivity match the original
- Refactor prop names for clarity and document required vs. optional props using PropTypes or TypeScript
Code
// Before: Repeated code in multiple files
// In Page1.jsx
<div className="product-card">
<img src={product.image} alt={product.name} />
<h3>{product.name}</h3>
<p className="price">${product.price}</p>
<button onClick={() => addToCart(product.id)}>Add to Cart</button>
</div>
// After: Extract into ProductCard.jsx
import PropTypes from 'prop-types';
function ProductCard({ image, name, price, onAddToCart, productId }) {
return (
<div className="product-card">
<img src={image} alt={name} />
<h3>{name}</h3>
<p className="price">${price}</p>
<button onClick={() => onAddToCart(productId)}>Add to Cart</button>
</div>
);
}
ProductCard.propTypes = {
image: PropTypes.string.isRequired,
name: PropTypes.string.isRequired,
price: PropTypes.number.isRequired,
onAddToCart: PropTypes.func.isRequired,
productId: PropTypes.number.isRequired,
};
export default ProductCard;
// Usage in Page1.jsx and Page2.jsx:
import ProductCard from './components/ProductCard';
<ProductCard
image={product.image}
name={
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 Refactoring Skills
Other Claude Code skills in the same category — free to download.
Extract Function
Extract code blocks into well-named functions
Rename Symbol
Safely rename variables, functions, classes across codebase
Remove Dead Code
Find and remove unused code
Simplify Conditionals
Simplify complex if/else chains and nested conditions
DRY Refactor
Find code duplication and extract shared logic
Promise to Async
Convert Promise chains to async/await
Class to Functions
Convert class-based code to functional style
Callback to Promise
Convert callback-based code to Promises
Want a Refactoring 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.