Writing clean React code isn't just about making things work β itβs about readability, maintainability, and scaling your components.
Hereβs a simple visual cheatsheet that highlights best practices for building better React components.
β Doβs & Donβts
πΈ Props
β Bad:
<MyButton txt="Click" col="blue" />
β
Good:
<MyButton label="Click" color="primary" />
π‘ Use meaningful, descriptive prop names.
πΈ Conditional Rendering
β Bad:
{!isLoading ? <Data /> : null}
β
Good:
{isLoading && <Spinner />}
π‘ Use early returns and avoid nesting where possible.
πΈ Folder Structure
β Bad:
/components
Button.js
Card.js
Modal.js
β
Good:
/components
/Button
index.tsx
styles.module.css
types.ts
π‘ Organize by component, not by file type.
πΈ Logic Separation
β Bad:
const fetchData = async () => {
const res = await fetch(...);
const data = await res.json();
setState(data);
};
β
Good:
import { getUserData } from '@/services/user';
const user = await getUserData();
setUser(user);
π‘ Keep logic in services or hooks. Components should be mostly UI.
πΈ useEffect Usage
β Bad:
useEffect(() => {
window.addEventListener('resize', handleResize);
}, []);
β
Good:
useEffect(() => {
window.addEventListener('resize', handleResize);
return () => window.removeEventListener('resize', handleResize);
}, []);
π‘ Always clean up your effects to avoid memory leaks.
π Final Tip
Clean code in React isnβt just for you β itβs for your teammates, your future self, and the community. Refactor small, commit often, and prioritize clarity over cleverness.
Top comments (0)