useEffect cleanup function explained

Asked by Kevin O'Brien Jul 26, 2025 intermediate 1057 views
62

I don't understand the cleanup function in React's useEffect. When does it run and why do I need it?

useEffect(() => {
  // setup
  return () => {
    // cleanup - when does this run?
  };
}, [dependency]);

Can someone explain with practical examples?

Solutions

0 answers

No solutions yet

Be the first to help solve this problem!