reactjs / reactjs/react.dev

Possible conceptual pitfall when discussing accessing a state variable set in a previous render

Aperta
#5,782 0 commenti 1 reazione 0 assegnatari Vedi su GitHub

Nessuno ha ancora preso questa issue.

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

Descrizione

Hi there!

In this section: https://react.dev/learn/you-might-not-need-an-effect#adjusting-some-state-when-a-prop-changes

It took me a while to understood that prevItems would not be changed until setItems was invoked on it. What tripped me there was that I thought that useState(X) always set items to X on every re-render. Once I understood that prevItems is set to X only when the state is initialized, everything is clear.

This might perhaps be a good place in the docs to clarify (either on a couple of sentences or on its own Pitfall section) that setState will only initialize a value, but not update it during a re-render.

I take this opportunity for congratulating the team on the new React docs. There are one of the best technical documentation pieces I've read in my entire career. 🚀

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

Leggi la sezione collegata “Adjusting some state when a prop changes” nella documentazione di React e analizza la spiegazione di useState e setState tra un render e l’altro. Il lavoro è completato quando la sezione distingue chiaramente l’inizializzazione iniziale dello stato dagli aggiornamenti successivi e affronta il malinteso segnalato.

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à
Ferma
Chiarezza
Specificata chiaramente
Idoneità per principianti
55/100

Ricevi le nuove issue nella tua casella

Un breve riepilogo di issue GitHub adatte ai principianti.