reactjs / reactjs/react.dev

Derived State: Fully uncontrolled component with a key

Offen
#1,056 0 Kommentare 6 Reaktionen 0 zugewiesene Personen Auf GitHub ansehen

Dieses Issue hat noch niemand übernommen.

Vorherrschende Sprache
JavaScript
Sterne
11.8k
Forks
7.9k
Ø Merge
1 T. 11 Std.
Gemergte PRs (30 T.)
11

Beschreibung

Hi,

In the article You Probably Don't Need Derived State, one of the recommended ways to avoid deriving state from props, is to have "Fully uncontrolled component with a key".

However, there is no way to actually force the user to provide a 'key' property to the component, since declaring it in propTypes as "key: PropTypes.any.isRequired" won't work, because it's value is always 'undefined', which leads to a warning message:

Warning: MyComponent: key is not a prop. Trying to access it will result in undefined being returned. If you need to access the same value within the child component, you should pass it as a different prop.

And without declaring the 'key' prop type, we cannot assume, that the user will use the component in a proper manner.

How to mitigate this?

Beitragsleitfaden

Beitragsleitfaden öffnen

Erste Schritte

  1. Lies das ganze Issue und danach den Beitragsleitfaden des Projekts.
  2. Schreib ins Issue, dass du es übernimmst — das erspart doppelte Arbeit.
  3. Forke das Repository und arbeite in einem Branch.
  4. Öffne einen Pull Request, der die Issue-Nummer nennt.

Rechercherichtung

Beginne mit dem verlinkten Artikel „You Probably Don't Need Derived State“ und der gemeldeten PropTypes-Warnung über den Zugriff auf key. Ermittle, ob die Dokumentation zu vollständig unkontrollierten Komponenten mit einem key einer Klarstellung bedarf; abgeschlossen ist die Aufgabe, wenn der Artikel eine gültige Abhilfe erklärt oder die Einschränkung ausdrücklich dokumentiert.

Vom Indexierungsmodell aus dem Issue-Text verfasst.

Bewertung

Tech-Stack
javascript, react
Bereich
documentation
Issue-Typ
Dokumentation
Schwierigkeit
3/5
Geschätzter Aufwand
1-2 Tage
Aktivitätsstatus
Veraltet
Klarheit
Muss geklärt werden
Anfängerfreundlichkeit
25/100

Neue Issues direkt in Ihr Postfach

Eine kurze Übersicht über anfängerfreundliche GitHub-Issues.