ReactJS Code Mistakes & Power Enhancements

πŸš«βš›οΈ ReactJS Code Mistakes & Power Enhancements Every Developer Should Know! βš‘πŸ’‘

ReactJS is a developer’s best friend β€” fast, efficient, and flexible. But even pros can slip up! πŸ€¦β€β™‚οΈ Whether you’re a beginner or a seasoned dev, avoiding common pitfalls and using best practices can supercharge your React apps πŸš€.

hq720 (2)

Let’s dive into some React mistakes you should avoid β€” and some enhancements you must adopt! 🧠πŸ’ͺ


1️⃣ ❌ Mutating State Directly

🚨 Mistake:

const [user, setUser] = useState({ name: 'John', age: 30 });

user.age = 31; // ❌ Direct mutation
setUser(user); // Won't re-render!

βœ… Enhancement:

setUser((prev) => ({ ...prev, age: 31 })); // βœ”οΈ Immutable update

πŸ” Why it matters:

React relies on immutability to detect changes. Mutating state directly can lead to stale UI and bugs that are very hard to track!


2️⃣ πŸŒ€ Overusing useEffect

🚨 Mistake:

useEffect(() => {
  fetchData();
}, [data]); // ❌ Causes infinite loop

βœ… Enhancement:

useEffect(() => {
  fetchData();
}, []); // βœ”οΈ Fetch once on mount

πŸ’‘ Pro Tip:

Use useEffect only when needed. It’s not a dumping ground for all logic β€” it’s meant for side effects like fetching data or setting up subscriptions.


3️⃣ πŸ” Missing Keys in Lists

🚨 Mistake:

{items.map((item) => (
  <div>{item.name}</div> // ❌ No key
))}

βœ… Enhancement:

{items.map((item) => (
  <div key={item.id}>{item.name}</div> // βœ”οΈ Unique key
))}

🎯 Why it matters:

React uses keys to efficiently update the DOM. Without them, your app might behave unpredictably πŸ˜΅β€πŸ’«.


4️⃣ 🧱 Too Many Props Drilling

🚨 Mistake:

Passing props from parent β†’ child β†’ grandchild β†’ … 🀯

<Grandparent user={user} />

All the way down to:

<Child user={user} />

βœ… Enhancement:

βœ… Use Context API or State Management like Redux, Zustand, or Jotai.

const UserContext = createContext();

function App() {
  return (
    <UserContext.Provider value={user}>
      <ComponentTree />
    </UserContext.Provider>
  );
}

function Child() {
  const user = useContext(UserContext);
  return <div>{user.name}</div>;
}

🌐 Bonus Tip:

πŸ”— Use useContext wisely β€” too many re-renders can occur if not optimized with memoization.


5️⃣ πŸ”§ Forgetting Memoization (Re-Renders Everywhere!)

🚨 Mistake:

const handleClick = () => { console.log("Clicked"); };

Used in child props β€” causes child to re-render on every parent render.

βœ… Enhancement:

const handleClick = useCallback(() => {
  console.log("Clicked");
}, []);

Or memoize the child:

const Child = React.memo(({ handleClick }) => {
  return <button onClick={handleClick}>Click</button>;
});

🧠 Why it matters:

React compares references. Without useCallback, new functions are created on every render β†’ unnecessary re-renders.


6️⃣ πŸ§ͺ Ignoring Component Reusability

🚨 Mistake:

Copy-pasting the same UI blocks everywhere πŸ”

βœ… Enhancement:

Break code into reusable, composable components:

const Button = ({ label, onClick }) => (
  <button onClick={onClick}>{label}</button>
);

// Use it anywhere!
<Button label="Submit" onClick={handleSubmit} />

🧱 Bonus Tip:

Build a small design system for reusable UI!


7️⃣ 🚦 Not Handling Loading & Errors

🚨 Mistake:

const [data, setData] = useState();

useEffect(() => {
  fetchData().then(setData);
}, []);

No loading or error state 😬

βœ… Enhancement:

const [data, setData] = useState(null);
const [loading, setLoading] = useState(true);
const [error, setError] = useState(null);

useEffect(() => {
  fetchData()
    .then((res) => setData(res))
    .catch(setError)
    .finally(() => setLoading(false));
}, []);

πŸŽ‰ Result:

βœ… Smooth UX with loading spinners and error fallbacks!


8️⃣ 🧹 Not Cleaning Up Side Effects

🚨 Mistake:

useEffect(() => {
  const interval = setInterval(() => { console.log("Tick"); }, 1000);
}, []);

No cleanup! Leads to memory leaks πŸ”₯

βœ… Enhancement:

useEffect(() => {
  const interval = setInterval(() => { console.log("Tick"); }, 1000);
  return () => clearInterval(interval); // βœ”οΈ Clean up
}, []);

9️⃣ πŸͺž Not Using PropTypes or TypeScript

🚨 Mistake:

const MyComponent = ({ name }) => <div>{name}</div>;
// No validation!

βœ… Enhancement:

import PropTypes from 'prop-types';

MyComponent.propTypes = {
  name: PropTypes.string.isRequired,
};

Or use TypeScript:

type MyComponentProps = {
  name: string;
};

const MyComponent = ({ name }: MyComponentProps) => <div>{name}</div>;

πŸ‘“ Why?

It saves hours of debugging and makes your components self-documenting!


πŸ”š Wrapping Up!

Mastering React is not just about writing code β€” it’s about writing smart, clean, and maintainable code πŸ§˜β€β™‚οΈπŸ’»

πŸ’₯ Here’s a quick recap:

βœ… Keep state immutable βœ… Don’t abuse useEffect βœ… Always add keys in lists βœ… Avoid props drilling βœ… Use memoization βœ… Reuse components βœ… Handle loading & errors βœ… Clean up effects βœ… Use PropTypes or TypeScript


✨ Let’s Level Up Together!

If this helped you, consider bookmarking πŸ”– and sharing with your dev friends πŸ‘¨β€πŸ’»πŸ‘©β€πŸ’»


πŸ“£ Let me know your worst React mistake in the comments! Let’s grow together πŸ’¬πŸ‘‡

© Lakhveer Singh Rajput - Blogs. All Rights Reserved.