DEV Community

Cover image for #πŸš€ React Clean Code Cheatsheet β€” Visual Guide
Samir Tahiri
Samir Tahiri

Posted on

#πŸš€ React Clean Code Cheatsheet β€” Visual Guide

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" />
Enter fullscreen mode Exit fullscreen mode

βœ… Good:

<MyButton label="Click" color="primary" />
Enter fullscreen mode Exit fullscreen mode

πŸ’‘ Use meaningful, descriptive prop names.


πŸ”Έ Conditional Rendering

❌ Bad:

{!isLoading ? <Data /> : null}
Enter fullscreen mode Exit fullscreen mode

βœ… Good:

{isLoading && <Spinner />}
Enter fullscreen mode Exit fullscreen mode

πŸ’‘ Use early returns and avoid nesting where possible.


πŸ”Έ Folder Structure

❌ Bad:

/components
  Button.js
  Card.js
  Modal.js
Enter fullscreen mode Exit fullscreen mode

βœ… Good:

/components
  /Button
    index.tsx
    styles.module.css
    types.ts
Enter fullscreen mode Exit fullscreen mode

πŸ’‘ Organize by component, not by file type.


πŸ”Έ Logic Separation

❌ Bad:

const fetchData = async () => {
  const res = await fetch(...);
  const data = await res.json();
  setState(data);
};
Enter fullscreen mode Exit fullscreen mode

βœ… Good:

import { getUserData } from '@/services/user';

const user = await getUserData();
setUser(user);
Enter fullscreen mode Exit fullscreen mode

πŸ’‘ Keep logic in services or hooks. Components should be mostly UI.


πŸ”Έ useEffect Usage

❌ Bad:

useEffect(() => {
  window.addEventListener('resize', handleResize);
}, []);
Enter fullscreen mode Exit fullscreen mode

βœ… Good:

useEffect(() => {
  window.addEventListener('resize', handleResize);
  return () => window.removeEventListener('resize', handleResize);
}, []);
Enter fullscreen mode Exit fullscreen mode

πŸ’‘ 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.


React Clean Code Guide

Top comments (0)