reactjs / reactjs/react.dev

Improve uncontrolled form components documentation

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

Referring to Uncontrolled Components I think there are some pitfalls to be documented when using them.


Rendering uncontrolled components correctly

In contrast to controlled components, uncontrolled components using defaultValue, defaultChecked should not be rendered until the default is present, respectively <select> should not be rendered until the default and the <option> children are present, because the functionality of the defaultValue or defaultChecked is only given after mounting, not on updating.

Not being rendered can be achieved in two different ways:

  1. simply don't render it e.g. return null
  2. force rerender with changing key on it

If you strictly don't want partial results in your form use "B", else "C" would work.

// A)
// bad
class UncontrolledMotd extends React.Component {

  state = {
    text: null
  };

  async componentDidMount() {
    const res = await fetch('/motd');
    const text = await res.text();
    this.setState({text});
  }

  render() {
    // bad default
    return (
      <input type="text" defaultValue={this.state.text} />
    );
  }
}
// B)
// better: no render, no placeholder
class UncontrolledMotd extends React.Component {

  state = {
    loaded: false,
    text: null
  };

  async componentDidMount() {
    const res = await fetch('/motd');
    const text = await res.text();
    this.setState({loaded: true, text});
  }

  render() {
    if (!this.state.loaded) {
      return null;
    }

    return (
      <input type="text" defaultValue={this.state.text} />
    );
  }
}
// C)
// better: placeholder, using key
class UncontrolledMotd extends React.Component {

  state = {
    loaded: false,
    text: null
  };

  async componentDidMount() {
    const res = await fetch('/motd');
    const text = await res.text();
    this.setState({loaded: true, text});
  }

  render() {
    return (
      <input key={!this.state.loaded ? 'preview' : 'final'} type="text" defaultValue={this.state.text} />
    );
  }
}

Did not test code above, but the idea in my current project and it works.
What do you think?

Regards Philipp

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 der Durchsicht der verlinkten Dokumentation zu Uncontrolled Components und der in diesem Issue vorgeschlagenen Beispiele. Überprüfe das Verhalten von unkontrollierten Komponenten und ermittle, wie die Fallstricke in die Dokumentation aufgenommen werden sollten; als abgeschlossen gilt die Aufgabe, wenn die relevanten Hinweise und getesteten Beispiele klar eingearbeitet sind.

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
Größtenteils klar
Anfängerfreundlichkeit
35/100

Neue Issues direkt in Ihr Postfach

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