reactjs / reactjs/react.dev

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

Open
#8,410 0 comments 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

type: documentation
Dominant language
JavaScript
Stars
11.8k
Forks
7.9k
Avg merge
1d 11h
Merged PRs (30d)
11

Description

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.

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Begin with the behavior described for useEffect, useCallback, and useMemo, and compare it with eslint-plugin-react-hooks' exhaustive-deps rule. Define a concrete runtime-warning scope and acceptance cases for omitted dependencies, dynamic values, and custom hooks; the issue is done when the warning behavior and validation approach are specified.

Written by the indexing model from the issue text.

Assessment

Tech stack
eslint, javascript, react
Domain
developer-experience, frontend, tooling
Issue type
Feature
Difficulty
5/5
Estimated time
Over a week
Activity status
Quiet
Clarity
Needs clarification
Newbie friendliness
35/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.