reactjs / reactjs/react.dev

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

Aperta Adatta ai principianti
#8,099 4 commenti 0 reazioni 0 assegnatari Vedi su GitHub

Nessuno ha ancora preso questa issue.

type: typos
Lingua principale
JavaScript
Stelle
11.8k
Fork
7.9k
Merge medio
1g 11h
PR unite (30g)
11

Descrizione

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.

Guida per i contributori

Apri la guida per i contributori

Come iniziare

  1. Leggi tutta la issue e poi la guida ai contributi del progetto.
  2. Commenta sulla issue per dire che te ne occupi tu — evita che due persone facciano lo stesso lavoro.
  3. Fai un fork del repository e lavora su un branch.
  4. Apri una pull request che faccia riferimento al numero della issue.

Direzione di ricerca

Apri la pagina “You Might Not Need an Effect” ed esamina Challenge 2, in particolare le spiegazioni su useMemo e NewTodo. Verifica il comportamento delle dipendenze descritto nell’issue, quindi rivedi la formulazione in modo che distingua correttamente l’implementazione originale dalle alternative; il lavoro è completato quando la spiegazione non implica più che l’implementazione originale ricalcoli a ogni modifica dell’input.

Scritto dal modello di indicizzazione a partire dal testo della issue.

Valutazione

Stack tecnologico
javascript, react
Ambito
documentation
Tipo di issue
Documentazione
Difficoltà
2/5
Tempo stimato
1-3 ore
Stato di attività
Tranquilla
Chiarezza
Specificata chiaramente
Idoneità per principianti
75/100

Ricevi le nuove issue nella tua casella

Un breve riepilogo di issue GitHub adatte ai principianti.