DEV Community

Tilak Jain
Tilak Jain

Posted on

5 Common Mistakes Beginners Make with React Hooks

React Hooks have revolutionized the way we write React components, but they come with their own set of challenges. Here are five common mistakes beginners often make when working with React Hooks.

1. Breaking the Rules of Hooks

One of the most critical mistakes is violating the Rules of Hooks. These rules state that:

  • Only call Hooks at the top level (not inside loops, conditions, or nested functions)
  • Only call Hooks from React function components or custom Hooks
// ❌ Wrong - Hook inside condition
if (condition) {
  const [state, setState] = useState(0);
}

// ✅ Correct - Hook at top level
const [state, setState] = useState(0);
if (condition) {
  // Use state here
}
Enter fullscreen mode Exit fullscreen mode

2. Forgetting Dependencies in useEffect

Another common mistake is omitting dependencies in the useEffect dependency array. This can lead to stale closures and bugs that are hard to track down.

// ❌ Wrong - Missing dependency
useEffect(() => {
  console.log(count);
}, []);

// ✅ Correct - Include all dependencies
useEffect(() => {
  console.log(count);
}, [count]);
Enter fullscreen mode Exit fullscreen mode

3. Overusing useState for Complex State

When your state logic becomes complex, using multiple useState calls can make your code harder to manage. Consider using useReducer instead.

// ❌ Not ideal - Multiple related states
const [name, setName] = useState('');
const [age, setAge] = useState(0);
const [email, setEmail] = useState('');

// ✅ Better - Single state object with useReducer
const [state, dispatch] = useReducer(reducer, {
  name: '',
  age: 0,
  email: ''
});
Enter fullscreen mode Exit fullscreen mode

4. Not Cleaning Up Effects

Failing to clean up side effects can lead to memory leaks and unexpected behavior, especially with subscriptions, timers, or event listeners.

// ❌ Wrong - No cleanup
useEffect(() => {
  const timer = setInterval(() => {
    console.log('tick');
  }, 1000);
}, []);

// ✅ Correct - Proper cleanup
useEffect(() => {
  const timer = setInterval(() => {
    console.log('tick');
  }, 1000);

  return () => clearInterval(timer);
}, []);
Enter fullscreen mode Exit fullscreen mode

5. Confusing useCallback and useMemo

Many beginners struggle to understand when to use useCallback versus useMemo. Remember:

  • useMemo memoizes the result of a function
  • useCallback memoizes the function itself
// useMemo - memoizes the computed value
const expensiveValue = useMemo(() => {
  return computeExpensiveValue(a, b);
}, [a, b]);

// useCallback - memoizes the function
const handleClick = useCallback(() => {
  doSomething(a, b);
}, [a, b]);
Enter fullscreen mode Exit fullscreen mode

Conclusion

React Hooks are powerful tools, but they require careful attention to these common pitfalls. By understanding and avoiding these mistakes, you'll write cleaner, more maintainable React code. Keep practicing, and these patterns will become second nature!

Have you encountered any of these mistakes in your React journey? Share your experiences in the comments below!

Top comments (0)