reactjs / reactjs/react.dev

Confusing explanation in Challenge 2 of React Learn ("You Might Not Need an Effect")

オープン 初心者向け
#8,099 コメント 4 件 リアクション 0 件 担当者 0 名 GitHub で見る

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

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

説明

Summary

The explanation in Challenge 2 (Cache a calculation without Effects) of You Might Not Need an Effect is confusing and somewhat misleading. The challenge suggests that replacing the useEffect with useMemo (or extracting a NewTodo component) reduces the number of calls to getVisibleTodos(), but this isn’t accurate. The original code already avoids unnecessary recomputations.

Page

https://react.dev/learn/you-might-not-need-an-effect

Details

In the original implementation, getVisibleTodos() is only called when either todos or showActive change. Typing in the input only updates the text state, which isn’t part of the dependency array—so getVisibleTodos() is not called on every keystroke.

The proposed useMemo solution behaves the same way: it recalculates getVisibleTodos() when todos or showActive change, and not when text changes. As a result, it doesn’t reduce the number of calls compared to the original implementation. Console logs confirm this.

The second suggested approach (extracting NewTodo into a separate component) gives the impression that the initial code was calling getVisibleTodos() on every input change, but that’s incorrect. The original code was already optimized since text wasn’t part of the effect’s dependencies.

The documentation even says:

“This approach satisfies the requirements too. When you type into the input, only the text state variable updates. Since the text state variable is in the child NewTodo component, the parent TodoList component won’t get re-rendered. This is why getVisibleTodos() doesn’t get called when you type. (It would still be called if the TodoList re-renders for another reason.)”

However, this explanation is misleading because getVisibleTodos() wasn’t being called at every input in the first place.

In short:

The useMemo version doesn’t reduce calls to getVisibleTodos(), it's exactly the same.

The second version’s explanation implies a problem that didn’t exist in the original code.

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

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

はじめの一歩

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

調査の方向性

「You Might Not Need an Effect」ページを開き、特に useMemo と NewTodo の説明を中心に Challenge 2 を確認してください。Issue に記載されている依存関係の挙動を検証し、そのうえで、元の実装と代替案を正確に区別できるように文言を修正してください。説明が、元の実装が入力の変更ごとに再計算することを示唆しなくなれば完了です。

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

評価

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

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

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