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)