reactjs / reactjs/react.dev

Prefer functional updates version in hooks documentation

Offen
#1,967 0 Kommentare 0 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

Currently React hooks documentation at section Updating state is using a counter example for bot hooks and class based components. To recap:

In a class, we need to call this.setState() to update the count state:

  <button onClick={() => this.setState({ count: this.state.count + 1 })}>
    Click me
  </button>

In a function, we already have setCount and count as variables so we don’t need this:

  <button onClick={() => setCount(count + 1)}>
    Click me
  </button>

Although I can understand than this documentation must be simple and this example is really straightforward, isn't this a bad patter? Modifying state with a value based on old state?

The Functional update version is documented few pages after this example.

I'm wondering if:

  • this example must be changed (although is probably the best type of example for state), or
  • a warning box should be added to warn the user this is not the correct way to do.

To be more clear: I know there's nothing wrong with this specific example, but newcomers can quickly try to do something like calling setCount(count + 1) multiple times.

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 Abschnitt Updating state in hooks-state.html und vergleiche sein Zählerbeispiel mit dem Abschnitt Functional updates in hooks-reference.html. Bestimme, ob das Beispiel oder ein erklärender Warnhinweis wiederholte Aktualisierungen auf Grundlage des vorherigen Zustands besser behandelt; abgeschlossen ist die Aufgabe, wenn die Dokumentation Einsteiger klar anleitet, ohne dem bestehenden Beispiel zu widersprechen.

Vom Indexierungsmodell aus dem Issue-Text verfasst.

Bewertung

Tech-Stack
javascript, react
Bereich
documentation
Issue-Typ
Dokumentation
Schwierigkeit
2/5
Geschätzter Aufwand
1-3 Stunden
Aktivitätsstatus
Veraltet
Klarheit
Größtenteils klar
Anfängerfreundlichkeit
45/100

Neue Issues direkt in Ihr Postfach

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