[Mistake]: set-state-in-effect gives a wrong example of bad usage

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

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

評価

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

調査の方向性

src/content/reference/eslint-plugin-react-hooks/lints/set-state-in-render.mdを読み、特にリンク先の70–91行と「I want to sync state to a prop」セクションを確認してください。Counterの例とその主張が、issueで説明されている挙動と一致するか確認してください。完了とは、例と説明が報告されたケースを正確に説明している状態です。

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

説明

type: typos
Summary

https://github.com/reactjs/react.dev/blob/abe931a8cb3aee3e8b15ef7e187214789164162a/src/content/reference/eslint-plugin-react-hooks/lints/set-state-in-render.md?plain=1#L70-L91

Page

https://react.dev/reference/eslint-plugin-react-hooks/lints/set-state-in-render

Details
I want to sync state to a prop {/clamp-state-to-prop/}

A common problem is trying to "fix" state after it renders. Suppose you want to keep a counter from exceeding a max prop:

// ❌ Wrong: clamps during render
function Counter({max}) {
  const [count, setCount] = useState(0);

  if (count > max) {
    setCount(max);
  }

  return (
    <button onClick={() => setCount(count + 1)}>
      {count}
    </button>
  );
}

As soon as count exceeds max, an infinite loop is triggered.

I think this Counter component can be rendered and clicked without issues.
The claim that an infinite loop will be triggered is incorrect.

主要言語
JavaScript
スター
11.8k
フォーク
7.9k
平均マージ
16時間 6分
マージ済み PR(30日)
7

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

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

はじめの一歩

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

reactjs/react.dev のほかの issue

reactjs/react.dev の issue をすべて見る

似ている issue

JavaScript の issue をもっと見る

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

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