有什麼更好?反應 - 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,第二個。 – Nick