Skip to content

Commit 858fae4

Browse files
authored
docs: add more examples to web-api/no-leaked-event-listener docs (#1082)
1 parent 47120ae commit 858fae4

File tree

16 files changed

+76
-15
lines changed

16 files changed

+76
-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: 61 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -253,6 +253,67 @@ function useCustomHook() {
253253
}
254254
```
255255

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

258319
- [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)