reactjs / reactjs/react.dev

Inconsistent usage of `.useState` in "Using the State Hook" article

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

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

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

説明

Hello! 👋

This is my first contribution to the React Documentation, and I first wanted to say how much I love the documentation. I find the language style used throughout it to be simple, accessible, and therefore easy to follow.

As I was going through the documentation this weekend, I made my way from State and Lifecycle to Using the State Hook. In the former article, I noted the following piece of information, which I thought was an important detail about updating state.

https://github.com/reactjs/reactjs.org/blob/6682068641c16df6547b3fcdb7877e71bb0bebf9/content/docs/state-and-lifecycle.md#L341-L345

Meanwhile, in the "Component Class Equivalent" section in the latter document, I noticed this code example did not use a function to update state using the previous state value.

https://github.com/reactjs/reactjs.org/blob/6682068641c16df6547b3fcdb7877e71bb0bebf9/content/docs/hooks-state.md#L37-L57

I found this confusing. The point above about accommodating a possibly asynchronous state update seemed critical to ensure deterministic behaviour. I was surprised to see the code here (and throughout other parts of this article) not using a function to update the state value synchronously.

There may be a few reasons why this was done in my mind.

  • The example is small/trivial enough that using a function versus a direct update does not make a major difference.
  • This is a minor implementation detail not relevant to the usage of the state hook.

It's also possible I may have just misunderstood this example. I believe though that is not only inconsistent, but also makes the function component and class component inequivalent, because the original is completely synchronous, whereas the latter contains some potentially asynchronous behaviour.

Of course this is easily fixed by converting a line like

this.setState({ count: this.state.count + 1 })

to

this.setState((state) => ({ count: state.count + 1 })

I'm writing this first and foremost with the intention to clear up confusion that I experienced, and that others may as well. But I also want to ensure that my thinking is correct here!

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

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

はじめの一歩

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

調査の方向性

まず、content/docs/state-and-lifecycle.md と content/docs/hooks-state.md の例を比較します。特に、状態の更新と Component Class Equivalent に関するリンク先のセクションを確認してください。例で関数型更新を使うべきか、またはその違いを説明すべきかを検討し、その後、関係と動作が明確で一貫したものになるよう記事を更新します。

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

評価

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

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

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