Improve uncontrolled form components documentation
まだ誰も着手していません。
- 主要言語
- JavaScript
- スター
- 11.8k
- フォーク
- 7.9k
- 平均マージ
- 1日 11時間
- マージ済み PR(30日)
- 11
説明
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
コントリビューションガイド
はじめの一歩
- issue を最後まで読み、次にプロジェクトのコントリビューションガイドを読みます。
- 着手することを issue にコメントします — 二人が同じ作業をするのを防げます。
- リポジトリをフォークし、ブランチを切って変更します。
- issue 番号を参照したプルリクエストを送ります。
調査の方向性
リンクされている Uncontrolled Components のドキュメントと、この issue で提案されている例をまず確認してください。非制御コンポーネントの動作を検証し、落とし穴をドキュメントにどのように組み込むべきかを判断してください。関連するガイダンスとテスト済みの例が明確に組み込まれれば完了です。
索引モデルが issue の本文から書いたものです。
評価
- 技術スタック
- javascript, react
- 領域
- documentation
- issue の種類
- ドキュメント
- 難易度
- 3/5
- 見積もり時間
- 1〜2日
- 活発さ
- 停滞
- 明瞭さ
- おおむね明確
- 初心者へのやさしさ
- 35/100