reactjs / reactjs/react.dev

Controlled form components with async data

オープン
#1,238 コメント 0 件 リアクション 1 件 担当者 0 名 GitHub で見る

まだ誰も着手していません。

主要言語
JavaScript
スター
11.8k
フォーク
7.9k
平均マージ
1日 11時間
マージ済み PR(30日)
11

説明

This seems like a pretty common scenario when implementing forms with React+Redux but neither documentation nor any of the articles/answers online seem to provide full implementation/recommendation.

Here's the scenario

  1. Data is loaded initially and passed to my <Form /> component.
class Form extends React.Component {
    constructor(props) {
        this.state = { nameField: props.name };
    }
    ...
}
  1. Initial data is rendered on the screen and state is used to track user input, making this a fully controlled component.
render() {
    return(
        <form>
            <input value={this.state.nameField} onChange={e => this.setState({ nameField: e.target.value })} />
        </form>
    );
}
  1. Data is then saved to the server asynchronously (here I'm using Redux but the same thing can be achieved with fetch, etc.):
handleSubmit(e) {
    e.preventDefault();
    const { dispatch } = this.props;
    dispatch(actions.saveData(...this.state);
}
  1. Reducers handle successful data save and the new state is saved. This data is then once again passed to my <Form /> component as props because the redux state is my single source of truth.

I now should update my component's state to match the application state (in case there were some external changes done on the server or parent component). Since in this case "fresh" props should always overwrite the state, one would normally use componentWillReceiveProps to simply set the new state but that method is now deprecated. The other two methods available - componentDidUpdate and getDerivedStateFromProps - could work BUT there needs to be a deep comparison of new props vs old props done in order to determine if the change is due to "fresh" props or just the internal state update. This can get very expensive if my form has 30 fields because both of these methods fire on each re-render.

I am trying to figure out the best practice in this scenario - should I delegate user input changes to Redux and avoid using component's state altogether? Or the only way now to make sure that the state stays in sync with props is to do a deep comparison of new pros vs old props on each re-render? Or is there another approach I'm not thinking of that works better?

コントリビューションガイド

コントリビューションガイドを開く

はじめの一歩

  1. issue を最後まで読み、次にプロジェクトのコントリビューションガイドを読みます。
  2. 着手することを issue にコメントします — 二人が同じ作業をするのを防げます。
  3. リポジトリをフォークし、ブランチを切って変更します。
  4. issue 番号を参照したプルリクエストを送ります。

調査の方向性

この issue では、リポジトリのファイル、テスト、エントリーポイントが指定されていません。まず、react.dev で非同期に読み込まれるデータを使用する制御されたフォームをドキュメント化すべきかどうかを明確にし、意図するガイダンスを定義してください。完了条件は、説明されている React と Redux のシナリオに答えるドキュメントです。

索引モデルが issue の本文から書いたものです。

評価

技術スタック
javascript, react
領域
documentation
issue の種類
ドキュメント
難易度
5/5
見積もり時間
1週間以上
活発さ
停滞
明瞭さ
説明が足りない
初心者へのやさしさ
25/100

新しい issue をメールで受け取る

初心者向けの GitHub issue を短くまとめたダイジェスト。