Rules
no-leaked-event-listener
Full Name in eslint-plugin-react-web-api
Full Name in @eslint-react/eslint-plugin
Presets
web-apirecommendedrecommended-typescriptrecommended-type-checked
Description
Enforces that every addEventListener in a component or custom Hook has a corresponding removeEventListener.
Adding an event listener without removing it can lead to memory leaks and unexpected behavior. This is because the event listener will continue to exist even after the component or hook is unmounted.
Examples
Failing
Passing
Failing
The following cases are intentionally considered as possible leaks:
Passing
Instead, you should always use the same entries in both the setup and cleanup functions: