Back to course

useEffect Cleanups and Dependencies

React JS 0 to Hero: The Complete Guide

Mastery of useEffect

  • Dependencies: React re-runs the effect when items in the dependency array change.
  • Cleanup: Return a function to clean up subscriptions or timers to prevent memory leaks.

jsx useEffect(() => { const timer = setInterval(() => {}, 1000); return () => clearInterval(timer); }, []);