reactjs / reactjs/react.dev

[Documentation] Add information about `child.key` to `React.Children.map`

Aperta
#2,023 0 commenti 0 reazioni 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

I just spent quite a while debugging a performance issue when reordering a large list of components, which were wrapped in React.Children.map.

The issue was the implementation of the wrapping component, which used the index as the key, which caused the entire list to re-render from scratch:

<div>
  {React.Children.map(props.children, (child, index) => (
    <div key={index} className='...'>{child}</div>
  ))}
</div>

The solution was to use child.key and pass it into the child-component, which then, in turn, made the memoization of the child components work as expected:

<div>
  {React.Children.map(props.children, (child) => (
    <div key={child.key} className='...'>{child}</div>
  ))}
</div>

I feel like this should be mentioned somewhere on the documentation page for React.Children.map, since I only figured out that this is possible at all after specifically searching for it and finding #10432.

I'm happy to update the docs in a PR if this gets the OK :)

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

Inizia con la sezione React.Children.map nella pagina della documentazione dell’API React collegata nell’issue e consulta la discussione correlata nell’issue #10432. Aggiungi informazioni chiare su child.key e sulla sua rilevanza quando si esegue il wrapping dei children, quindi verifica che la documentazione spieghi l’utilizzo previsto e la considerazione relativa alle prestazioni.

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.