Skip to content

React Memo and useCallback Error Fix

DodaTech Updated 2026-06-24 2 min read

In this tutorial, you'll learn about React Memo and useCallback Error Fix. We cover key concepts, practical examples, and best practices.

The Problem

React.memo does not prevent a child component from re-rendering even when props appear unchanged.

Wrong

const Child = React.memo(({ onClick }) => {
  console.log('Child rendered')
  return <button onClick={onClick}>Click</button>
})

function Parent() {
  const [count, setCount] = useState(0)

  // New function every render
  const handleClick = () => setCount(c => c + 1)

  return (
    <div>
      <Child onClick={handleClick} />
      <p>Count: {count}</p>
    </div>
  )
}

Output: console logs Child rendered every time count changes, even though the child is memoized.

Wrap the callback in useCallback:

const Child = React.memo(({ onClick }) => {
  console.log('Child rendered')
  return <button onClick={onClick}>Click</button>
})

function Parent() {
  const [count, setCount] = useState(0)

  const handleClick = useCallback(() => {
    setCount(c => c + 1)
  }, []) // Stable reference — never changes

  return (
    <div>
      <Child onClick={handleClick} />
      <p>Count: {{ count }}</p>
    </div>
  )
}

Expected output: Child renders only once. useCallback provides a stable function reference across re-renders.

Prevention

  • Wrap callbacks passed to memoized children with useCallback
  • Ensure React.memo has a proper comparison function for complex props
  • Profile re-renders with React Devtools before optimizing

Common Mistakes with memo use callback

  1. Placing the wildcard pattern first in case expressions, making all subsequent patterns unreachable
  2. Using head and tail instead of pattern matching, causing runtime errors on empty lists
  3. Forgetting that lazy evaluation defers computation until the value is forced, causing space leaks with unevaluated thunks

These mistakes appear frequently in real-world REACT code. DodaTech's contributors have identified these patterns through analysis of open-source projects and production systems.

Practice Exercise

Write a pure function that safely divides two integers using Maybe, then test it with edge cases like division by zero and negative numbers.

This exercise reinforces the concepts covered in this guide. Try implementing it before checking online solutions.

FAQ

### What is the difference between React.memo and useMemo?

React.memo prevents re-rendering of an entire component. useMemo caches the result of a computation. Both use shallow comparison by default.

When should I NOT use React.memo?

Do not use React.memo on components that receive complex props that change often, or on components where the re-render is cheap. Memoization has a comparison cost.

Does useCallback prevent re-renders?

useCallback alone does not prevent re-renders. It provides a stable function reference. You must also use React.memo on the child component that receives the callback.

Built by the developers of DodaTech

Doda Browser, DodaZIP & Durga Antivirus Pro