reactjs / reactjs/react.dev

Docs regarding setState are in direct conflict with one another

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

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

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

説明

In Introducting Hooks, and several other places, there is a simple counter example that illustrates how to use the useState hook.

function Example() {
  // Declare a new state variable, which we'll call "count"
  const [count, setCount] = useState(0);

  return (
    <div>
      <p>You clicked {count} times</p>
      <button onClick={() => setCount(count + 1)}>
        Click me
      </button>
    </div>
  );
}

However, there happens to be another counter example in the Hooks API Reference for useState.

function Counter({initialCount}) {
  const [count, setCount] = useState(initialCount);
  return (
    <>
      Count: {count}
      <button onClick={() => setCount(initialCount)}>Reset</button>
      <button onClick={() => setCount(prevCount => prevCount - 1)}>-</button>
      <button onClick={() => setCount(prevCount => prevCount + 1)}>+</button>
    </>
  );
}

In this second example, a callback is being passed into the setState function, and the code is accompanied by the following language...

The ”+” and ”-” buttons use the functional form, because the updated value is based on the previous value.

This would imply that the first example (which is in a very prominent location in the docs by the way), is actually incorrect and is encouraging an anti-pattern.

I hope you can see how these two examples are in direct conflict with one another and might be causing confusion in the community!

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

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

はじめの一歩

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

調査の方向性

Introducing Hooks と Hooks API Reference のカウンターの例、特に functional-updates セクションを読み、setState に関する説明を比較してください。ドキュメントが各形式をいつ使うのが適切かについて明確な説明を一つ提供するように不整合を解消し、その後、リンクされた例と周辺のテキストが互いに矛盾しなくなっていることを確認してください。

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

評価

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

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

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