jsx-eslint/eslint-plugin-react

Feature request: abortController + no-leaked-event-listener

开放

#3,967 创建于 2025年11月21日

 (3 条评论) (0 个反应) (0 位负责人)JavaScript (2,731 个派生)batch import
bughelp wanted

仓库指标

星标
 (9,293 个星标)
PR 合并指标
 (PR 指标待抓取)

描述

i've been using this pattern to clean up event listeners lately:

useEffect(() => {
  const abortController = new AbortController();
  const { signal } = abortController;

  window.addEventListener(
    "click",
    (event) => {
      doSomething();
    },
    { signal },
  );

  window.addEventListener(
    "resize",
    (event) => {
      doSomethingElse();
    },
    { signal },
  );

  return () => {
    abortController.abort();
  };
}, []);

it makes the cleanup function much smaller (and harder to mess up), plus it helps with type inference (i.e. you don't need to explicitly type your event handlers).

i can obviously just disable the no-leaked-event-listener rule, but it would be nice if it could detect this pattern with a heuristic, and maybe even alert you e.g. if you forget to pass signal or forget to call abort().

贡献者指南