何が良いですか?リアクション - this.input.value対ハンドル変更
私は10入力のフォームを持っています。
this.input.value
を使用するか、変更を処理してstate
に保存する必要がありますか?ドキュメントから
handleChange(e) {
this.setState({input: e.target.value});
}
...
<input type="text" value={this.state.input} onChange={this.handleChange} />
または
onSubmit() {
const inputValue = this.input.value;
...
}
...
<input type="text" ref={(input) => {this.input = input;}} />
:
制御入力を設定する参考文献
There are a few good use cases for refs: Managing focus, text selection, or media playback. Triggering imperative animations. Integrating with third-party DOM libraries. Avoid using refs for anything that can be done declaratively.
まず、「this.input.value」は機能しません。 'this.refs'を使って入力ボックスを参照する必要があります。私は複雑さを回避するために状態を使用します –
これは達成しようとしていることに依存します:入力ごとに行われるべきアクションがあるか、すべての値が提出された後、 –
@TravelingTechGuy、もう1つ。 – Nick