MediumPerformance📖 Theory Question

What causes unnecessary re-renders and how do you diagnose them?

💡

Hint

Parent renders, new object/function props, context value changes — diagnose with React DevTools Profiler

Full Answer

Common causes:

  • Parent re-renders → all children re-render (even with same props, unless memoized)
  • New object/array/function created inline → reference changes → memo fails
  • Context value is a new object reference → all consumers re-render
  • State updates that produce the same value (React bails out after first redundant render)
// ❌ New object every render — breaks memo
  // new {} each time
 doWork()} />  // new fn each time
         // new [] each time

// ❌ Context providing unstable reference
function Provider({ children }) {
  const [user, setUser] = useState(null);
  return (
      {/* new object every render! */}
      {children}
    
  );
}

// ✅ Stable context value
const value = useMemo(() => ({ user, setUser }), [user]);

Diagnosis tools:

  • React DevTools → Profiler → record interaction → see what rendered and why
  • React DevTools → Components → highlight updates checkbox
  • why-did-you-render library — logs unnecessary renders with reasons
💡 Re-renders are not always a problem — React is fast. Optimize only when you measure a real performance issue. Premature memoization adds complexity and can introduce bugs.

More Performance Questions

EasyWhat are debounce and throttle? When do you use each?EasyWhat causes memory leaks in JavaScript and how do you detect them?EasyWhat is the difference between reflow and repaint, and how do you avoid layout thrashing?MediumWhat is lazy loading and code splitting?

Practice this in a timed sprint →

5 free questions, no signup required

⚡ Start Sprint