Hackforge Academy

Category: react

React best practices

Published on 09 Apr 2026

Explanation

Not using keys properly in lists is a common mistake in React. Keys help React identify which elements changed, improved performance, and prevent unexpected UI behavior.

Code:

// Incorrect
{items.map(item => (
  <li>{item.name}</li>
))}

// Correct
{items.map(item => (
  <li key={item.id}>{item.name}</li>
))}

Explanation

Mutating state directly instead of using the state setter function can cause unexpected behavior because React may not detect the change.

Code:

// Incorrect
state.count = state.count + 1;

// Correct
setCount(count + 1);

Explanation

Not using dependency arrays correctly in useEffect can cause infinite loops or missed updates. Always specify dependencies carefully.

Code:

// Incorrect
useEffect(() => {
  fetchData();
});

// Correct
useEffect(() => {
  fetchData();
}, []);

Explanation

Creating too many components inside a single file instead of splitting them into reusable components makes the code harder to maintain and scale.

Code:

// Better practice
src/
 ├── components/
 │    ├── Header.jsx
 │    ├── Footer.jsx
 │    └── Sidebar.jsx

Explanation

Not using reusable components leads to duplicated code and inconsistent UI. Always extract repeating UI elements into shared components for better maintainability 🚀

Code:

// Reusable component example
function Button({ label }) {
  return <button>{label}</button>;
}

🚀 Learn Spring Boot with real-world projects

💡 Build REST APIs step by step

🧠 Improve backend development skills

🎯 Get career-ready practical training

Join Our Free WhatsApp Community

Direct access to niche-specific mentors and peers on WhatsApp.

🐍

Python Community

Discuss Django, FastAPI, AI integration, and automation scripts with 15k+ developers.

Join Python Community
⚛️

React Community

Master Next.js, Framer Motion, and State Management. Share your latest UI components.

Join React Community

Java Community

Deep dives into Spring Boot, Microservices architecture, and high-performance backend ops.

Join Java Community