reactjs / reactjs/react.dev

[Bug]: Documentation for hooks: warning about infinite loop useEffect with state update is not prominent

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

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

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

説明

Summary

Description

The documentation for React Hooks (especially useEffect) mentions that updating state inside an effect without a proper dependency array can cause infinite loops, but this warning is not highlighted prominently. Many beginners miss this and end up in render loops.

Given how frequently this issue occurs, the documentation should emphasize it more clearly, perhaps with a bold warning and a simple example.

Example that causes confusion

import React, { useState, useEffect } from "react";

function MyComponent() {
  const [count, setCount] = useState(0);

  useEffect(() => {
    setCount(c => c + 1);
  }); // Missing dependency array

  return <div>{count}</div>;
}

Running this causes an infinite render loop because setCount triggers a state update on every render, re-invoking useEffect.

Actual behavior

  • Docs mention dependency arrays briefly, but the infinite loop risk is not highlighted enough.
  • Beginners often miss this and think React is “re-rendering infinitely” due to a bug.

Expected behavior

  • The official docs should include a bold, visible warning in the useEffect section, such as:

Warning: Omitting the dependency array causes the effect to run after every render, potentially leading to infinite loops if state or props are updated inside.

  • Include a small “bad vs. good” code example pair to clarify.

Why this matters

This is one of the most common pitfalls for new React developers. Making the warning prominent in official docs will help reduce confusion and GitHub issues related to infinite re-renders.

Environment

  • React version: 18.x (or whichever version is current)
  • Browser: Chrome 118
  • OS: Windows 11 / Ubuntu 22.04

Additional info

Several discussions online highlight this confusion; here’s one example:
https://www.reddit.com/r/reactjs/comments/oomo1w/what_are_the_biggest_issues_you_see_with_react_in/

Page

https://react.dev/reference/react/useEffect

Details

The warning could be moved closer to the main example or highlighted with a strong visual cue (e.g., bold text or a red caution box).
This would help prevent confusion among new React users who encounter infinite loop behavior.

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

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

はじめの一歩

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

調査の方向性

issue でリンクされている useEffect リファレンスページから始め、依存配列とメインの例が現在どのように説明されているかを確認します。無限ループの警告を目立つようにし、悪い例と良い例を簡潔に示す例を追加します。その後、依存配列なしでエフェクトから state を更新するリスクがページで明確に説明されていることを確認します。

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

評価

技術スタック
javascript, react
領域
documentation
issue の種類
ドキュメント
難易度
2/5
見積もり時間
1〜3時間
活発さ
停滞
明瞭さ
明確に書かれている
初心者へのやさしさ
52/100

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

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