Improve uncontrolled form components documentation
Nessuno ha ancora preso questa issue.
- Lingua principale
- JavaScript
- Stelle
- 11.8k
- Fork
- 7.9k
- Merge medio
- 1g 11h
- PR unite (30g)
- 11
Descrizione
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
Guida per i contributori
Apri la guida per i contributori
Come iniziare
- Leggi tutta la issue e poi la guida ai contributi del progetto.
- Commenta sulla issue per dire che te ne occupi tu — evita che due persone facciano lo stesso lavoro.
- Fai un fork del repository e lavora su un branch.
- Apri una pull request che faccia riferimento al numero della issue.
Direzione di ricerca
Inizia esaminando la documentazione collegata su Uncontrolled Components e gli esempi proposti in questa issue. Verifica il comportamento dei componenti non controllati e determina come i problemi dovrebbero essere integrati nella documentazione; il lavoro è completato quando le indicazioni pertinenti e gli esempi verificati sono stati incorporati chiaramente.
Scritto dal modello di indicizzazione a partire dal testo della issue.
Valutazione
- Stack tecnologico
- javascript, react
- Ambito
- documentation
- Tipo di issue
- Documentazione
- Difficoltà
- 3/5
- Tempo stimato
- 1-2 giorni
- Stato di attività
- Ferma
- Chiarezza
- Abbastanza chiara
- Idoneità per principianti
- 35/100