The cleanup function runs before the next effect (or on unmount). It’s used to tear down subscriptions, timers, or event listeners:
Without cleanup, the interval would keep running even after your component is gone, causing memory leaks.
0
0
asked 403 days ago
1
7
I know useEffect hooks can return a cleanup function, but I’m not sure when to use it. Can someone explain what it does and give an example?
The cleanup function runs before the next effect (or on unmount). It’s used to tear down subscriptions, timers, or event listeners:
Without cleanup, the interval would keep running even after your component is gone, causing memory leaks.
1