reactjs / reactjs/react.dev

Nowhere in the docs does it say that `this.refs` is a reserved property name

Ouverte Adaptée aux débutants
#4,681 0 commentaires 0 réactions 0 personnes assignées Voir sur GitHub

Personne n'a encore pris cette issue.

Langage dominant
JavaScript
Étoiles
11.8k
Forks
7.9k
Merge moyen
1 j 11 h
PR mergées (30 j)
11

Description

When creating multiple refs for a component class, it isn't unlikely that an unaware developer, wanting to add an array of refs to their component, would choose to use the property name this.refs.

It seems however that React uses this property name internally and so assigning to this.refs in the constructor will be irreversibly overwritten by an empty object {}. Other (not-obviously-reserved) names are fine, including singular this.ref.

I assume that this isn't a bug with react (although maybe?), but this constraint isn't mentioned explicitly in the Reactjs.org docs. The only mention of this.refs is a small section on the Refs page of the docs titled "Legacy API: String Refs", which does explain that this.refs was used in a deprecated API, but doesn't explicitly say that the name .refs is still reserved and can't be used.

It should probably be mentioned explicitly somewhere on that page, preferably both near the top and in that particular section. It took me quite a while of debugging to incidentally try a different name and stumble upon this issue. There are also several StackOverflow questions from other people asking why their refs aren't working when they are using this.refs but none of the people answering seem to be aware of this constraint.

Guide de contribution

Ouvrir le guide de contribution

Par où commencer

  1. Lisez l'issue en entier, puis le guide de contribution du projet.
  2. Signalez en commentaire que vous la prenez — cela évite que deux personnes fassent le même travail.
  3. Forkez le dépôt et travaillez sur une branche.
  4. Ouvrez une pull request qui référence le numéro de l'issue.

Piste de recherche

Commencez par la page Refs et sa section « Legacy API: String Refs », qui mentionne actuellement this.refs sans décrire explicitement la contrainte liée aux noms réservés. Mettez à jour la documentation vers le début et dans cette section afin que les lecteurs sachent que this.refs est réservé et ne doit pas être écrasé.

Rédigé par le modèle d'indexation à partir du texte de l'issue.

Évaluation

Stack technique
javascript, react
Domaine
documentation
Type d'issue
Documentation
Difficulté
2/5
Temps estimé
1-3 heures
Activité
À l'abandon
Clarté
Clairement spécifiée
Accessibilité débutants
65/100

Recevez les nouvelles issues par e-mail

Un résumé court des issues GitHub adaptées aux débutants.