Skip to content

Commit 79f387d

Browse files
committed
docs: add more examples to web-api/no-leaked-event-listener docs
1 parent 47120ae commit 79f387d

File tree

16 files changed

+65
-15
lines changed

16 files changed

+65
-15
lines changed

VERSION

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
1.48.3-next.1
1+
1.48.3-beta.1

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "@eslint-react/monorepo",
3-
"version": "1.48.3-next.1",
3+
"version": "1.48.3-beta.1",
44
"private": true,
55
"description": "Monorepo for eslint-plugin-react-[x, dom, web-api, hooks-extra, naming-convention].",
66
"keywords": [

packages/core/package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "@eslint-react/core",
3-
"version": "1.48.3-next.1",
3+
"version": "1.48.3-beta.1",
44
"description": "ESLint React's ESLint utility module for static analysis of React core APIs and patterns.",
55
"homepage": "https://github.com/Rel1cx/eslint-react",
66
"bugs": {

packages/plugins/eslint-plugin-react-debug/package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "eslint-plugin-react-debug",
3-
"version": "1.48.3-next.1",
3+
"version": "1.48.3-beta.1",
44
"description": "ESLint React's ESLint plugin for debugging related rules.",
55
"keywords": [
66
"react",

packages/plugins/eslint-plugin-react-dom/package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "eslint-plugin-react-dom",
3-
"version": "1.48.3-next.1",
3+
"version": "1.48.3-beta.1",
44
"description": "ESLint React's ESLint plugin for React DOM related rules.",
55
"keywords": [
66
"react",

packages/plugins/eslint-plugin-react-hooks-extra/package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "eslint-plugin-react-hooks-extra",
3-
"version": "1.48.3-next.1",
3+
"version": "1.48.3-beta.1",
44
"description": "ESLint React's ESLint plugin for React Hooks related rules.",
55
"keywords": [
66
"react",

packages/plugins/eslint-plugin-react-naming-convention/package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "eslint-plugin-react-naming-convention",
3-
"version": "1.48.3-next.1",
3+
"version": "1.48.3-beta.1",
44
"description": "ESLint React's ESLint plugin for naming convention related rules.",
55
"keywords": [
66
"react",

packages/plugins/eslint-plugin-react-web-api/package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "eslint-plugin-react-web-api",
3-
"version": "1.48.3-next.1",
3+
"version": "1.48.3-beta.1",
44
"description": "ESLint React's ESLint plugin for interacting with Web APIs",
55
"keywords": [
66
"react",

packages/plugins/eslint-plugin-react-web-api/src/rules/no-leaked-event-listener.md

Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -253,6 +253,56 @@ function useCustomHook() {
253253
}
254254
```
255255

256+
### Failing
257+
258+
The following cases are intentionally considered as possible leaks:
259+
260+
```tsx
261+
useEffect(() => {
262+
if (!el) {
263+
return;
264+
}
265+
266+
for (const [name, handler] of Object.entries(handlers)) {
267+
// ^^^^^^^^^^^^^^^^^^^^^^^^^
268+
// - The entries object are not guaranteed to be the same as the ones in the effect cleanup function.
269+
el.addEventListener(name, handler);
270+
}
271+
272+
return () => {
273+
for (const [name, handler] of Object.entries(handlers)) {
274+
// ^^^^^^^^^^^^^^^^^^^^^^^^^
275+
// - The entries object are not guaranteed to be the same as the ones in the effect setup function.
276+
el.removeEventListener(name, handler);
277+
}
278+
};
279+
}, [el]);
280+
```
281+
282+
### Passing
283+
284+
Instead, you should always use the same handlerEntries object in both the effect setup and cleanup functions.
285+
286+
```tsx
287+
useEffect(() => {
288+
if (!el) {
289+
return;
290+
}
291+
292+
const handlerEntries = Object.entries(handlers); // <- Use the same object
293+
294+
for (const [name, handler] of handlerEntries) {
295+
el.addEventListener(name, handler);
296+
}
297+
298+
return () => {
299+
for (const [name, handler] of handlerEntries) {
300+
el.removeEventListener(name, handler);
301+
}
302+
};
303+
}, [el]);
304+
```
305+
256306
## Implementation
257307

258308
- [Rule source](https://github.com/Rel1cx/eslint-react/tree/main/packages/plugins/eslint-plugin-react-web-api/src/rules/no-leaked-event-listener.ts)

packages/plugins/eslint-plugin-react-x/package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "eslint-plugin-react-x",
3-
"version": "1.48.3-next.1",
3+
"version": "1.48.3-beta.1",
44
"description": "A set of composable ESLint rules for for libraries and frameworks that use React as a UI runtime.",
55
"keywords": [
66
"react",

0 commit comments

Comments
 (0)