Trung BìnhReact iconReact

Memory leak trong React component, cách phát hiện và fix?

Phát hiện: Chrome DevTools > Memory tab, check heap snapshots.

Nguyên nhân:

  1. Quên cleanup subscriptions/timers trong useEffect.
  2. Event listeners không remove.
  3. Abort controller không cancel fetch

Fix: return cleanup function trong useEffect.

Pattern: const controller = new AbortController(); return () => controller.abort();

Xem toàn bộ React cùng filter theo level & chủ đề con.

Mở danh sách React