| name | react-components |
| description | Expert guidance for React component architecture, maintainability, and best practices. Use when building React components, reviewing component structure, optimizing component performance, or working with React hooks. Triggers on: react component, component architecture, react best practices, component size, react hooks. |
React Components
Expert guidance for React component architecture and maintainability.
Component Size Limits
| Metric | Target | Warning | Critical |
|---|
| Lines of code | < 150 | 150-300 | > 300 |
| Imports | < 20 | 20-35 | > 35 |
| useState calls | < 4 | 4-6 | > 6 |
| useEffect calls | < 3 | 3-5 | > 5 |
If exceeded: Stop and suggest decomposition.
Hook Patterns
AbortController for async useEffect
useEffect(() => {
const controller = new AbortController();
const fetchData = async () => {
try {
const result = await api.getData({ signal: controller.signal });
setData(result);
} catch (err) {
if (!controller.signal.aborted) {
setError(err);
}
}
};
fetchData();
return () => controller.abort();
}, [dependency]);
useWatch over watch()
const value = useWatch({ name: 'field', control });
const value = methods.watch();
Memoize schemas
const schema = useMemo(() => yup.object({
field: yup.string().required()
}), []);
const schema = yup.object({ field: yup.string().required() });
No components inside render
const InfoTrigger = memo(({ onClick }) => (
<button onClick={onClick}>Info</button>
));
const Component = () => {
const Trigger = () => <button>Info</button>;
return <Trigger />;
};
File Structure
ComponentName/
├── ComponentName.tsx # Logic (< 100 lines)
├── ComponentName.styled.ts # Styles only
├── ComponentName.types.ts # Types/interfaces
├── ComponentName.test.tsx # Tests
└── hooks/
└── useComponentData.ts # Extracted logic
Best Practices
- Keep components small - Under 150 lines
- Extract custom hooks - Reusable logic
- Use TypeScript - Proper type definitions
- Memoize expensive computations - useMemo, useCallback
- Handle cleanup - AbortController for async operations
Activation Keywords
react-components
react-components
react components
Tools Used
Instructions for Agents
- Read the task description carefully
- Follow the step-by-step process
- Use the appropriate tools
- Verify the results
Examples
Example 1: Basic Usage
User:
Agent:
Example 2: Advanced Usage
User:
Agent: