A cleanup function is returned by a React effect to undo or release resources created by that effect. Effects may return nothing or a function, but no other value is valid. section 6.1.4
Cleanup runs before an effect with changed dependencies runs again, and it may run during development when Strict Mode repeats effects. Treat it as part of the effect’s lifecycle rather than as an optional callback. section 3.1.2
Use these implementation rules:
- Return nothing when the effect creates no resource or when a guard prevents setup. section 6.1.4
- Keep effects and cleanup behavior predictable when Strict Mode invokes lifecycle work repeatedly during development. section 3.1.2
Cleaning up a timer
When a timer is running, create the interval and return a cleanup function that clears it. When it is not running, use a bare return instead of returning false.





