cleanup function

The function an Effect returns so React can undo what the setup did, such as removing a listener or cancelling a request.

In more detail

React runs it before the Effect runs again with new dependencies and once more when the component unmounts. Every resource opened in setup should be released here; Strict Mode runs an extra setup and cleanup in development to check that.

Examples

The overlay's effect sets body overflow to hidden and makes the app root inert; its cleanup restores the previous overflow and removes inert.
return () => window.removeEventListener("keydown", onKey);

Related terms

Taught in

References