reactjs / reactjs/react.dev

[Suggestion]: Runtime Development Warning for Stale Closures in useEffect, useCallback, and useMemo

オープン
#8,410 コメント 0 件 リアクション 0 件 担当者 0 名 GitHub で見る

まだ誰も着手していません。

type: documentation
主要言語
JavaScript
スター
11.8k
フォーク
7.9k
平均マージ
1日 11時間
マージ済み PR(30日)
11

説明

Summary

Make the waring for Stale Closures in useEffect, useCallback, and useMemo more detailed

Page

No response

Details

React hooks that accept dependency arrays (useEffect, useCallback, useMemo) rely on developers to manually list all reactive values that the hook's closure depends on. When a value is omitted from the dependencies array, the hook's closure continues to reference an outdated value from a previous render—a "stale closure". This leads to subtle, difficult‑to‑debug bugs such as:

Event handlers or effects reading old state/props.
Effects that should run on state changes never re‑executing.
Callbacks that always operate on stale data.
Currently, the eslint-plugin-react-hooks provides a static analysis rule (exhaustive‑deps) that warns about missing dependencies. However, this rule:

Cannot catch all cases (e.g., dynamic values, complex control flow, or custom hooks that obscure dependencies).
Only runs at lint time; developers may ignore or disable the warning.
Does not warn about actual runtime behavior—it cannot tell if a stale closure actually read an outdated value during a particular render.
As a result, developers often spend hours chasing down stale‑closure bugs, especially in larger codebases or when integrating third‑party hooks.

コントリビューションガイド

コントリビューションガイドを開く

はじめの一歩

  1. issue を最後まで読み、次にプロジェクトのコントリビューションガイドを読みます。
  2. 着手することを issue にコメントします — 二人が同じ作業をするのを防げます。
  3. リポジトリをフォークし、ブランチを切って変更します。
  4. issue 番号を参照したプルリクエストを送ります。

調査の方向性

useEffect、useCallback、useMemo について説明されている動作から始め、eslint-plugin-react-hooks の exhaustive-deps ルールと比較します。実行時警告の具体的な範囲と、省略された依存関係、動的な値、カスタムフックに対する受け入れケースを定義します。警告の動作と検証アプローチが仕様化された時点で、この issue は完了です。

索引モデルが issue の本文から書いたものです。

評価

技術スタック
eslint, javascript, react
領域
developer-experience, frontend, tooling
issue の種類
機能追加
難易度
5/5
見積もり時間
1週間以上
活発さ
静か
明瞭さ
説明が足りない
初心者へのやさしさ
35/100

新しい issue をメールで受け取る

初心者向けの GitHub issue を短くまとめたダイジェスト。