reactjs / reactjs/react.dev

Passing Arguments to Event Handlers using data-*

Aperta
#3,011 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

Currently the documentation https://reactjs.org/docs/handling-events.html mentions two ways how to handle events when the event handler function needs a parameter:

<button onClick={(e) => this.deleteRow(id, e)}>Delete Row</button>
<button onClick={this.deleteRow.bind(this, id)}>Delete Row</button>

As far as I understand, both variants create a new function on each render, which might lead to performance issues because of unnecessary re-renders (you might have to implement custom logic in shouldComponentUpdate). In the documentation:

We generally recommend binding in the constructor or using the class fields syntax, to avoid this sort of performance problem.

Not mentioned is using a "data'*" HTML attribute and then use that event property in the handler.
I would like to document this variant – or if it is not recommended, document that (and why) this is not recommended.

See also https://github.com/facebook/react/issues/1259

If you consider this worth documenting I will create a PR.

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 dalla pagina di documentazione handling-events e rivedi i due pattern esistenti per passare argomenti ai gestori degli eventi, quindi considera l’approccio data-* proposto insieme alla issue React collegata. Il lavoro è completato quando la pagina raccomanda o rifiuta chiaramente questa variante e spiega il motivo.

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

Valutazione

Stack tecnologico
javascript, react
Ambito
documentation
Tipo di issue
Documentazione
Difficoltà
3/5
Tempo stimato
1-2 giorni
Stato di attività
Ferma
Chiarezza
Da chiarire
Idoneità per principianti
35/100

Ricevi le nuove issue nella tua casella

Un breve riepilogo di issue GitHub adatte ai principianti.