Improve uncontrolled form components documentation
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,defaultCheckedshould 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 thedefaultValueordefaultCheckedis only given after mounting, not on updating.
Not being rendered can be achieved in two different ways:
- simply don't render it e.g.
return null - force rerender with changing
keyon 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
Erste Schritte
- Lies das ganze Issue und danach den Beitragsleitfaden des Projekts.
- Schreib ins Issue, dass du es übernimmst — das erspart doppelte Arbeit.
- Forke das Repository und arbeite in einem Branch.
- Ö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