可以通过 event.target.value
来获取输入框的值。具体操作可以参考以下示例代码:
import React, { Component } from 'react';
class App extends Component {
constructor(props) {
super(props);
this.state = {
inputValue: ''
};
}
handleInputChange = (event) => {
this.setState({
inputValue: event.target.value
});
}
render() {
return (
<div>
<input type="text" value={this.state.inputValue} onChange={this.handleInputChange} />
<p>{this.state.inputValue}</p>
</div>
);
}
}
export default App;
以上代码通过 value
将输入框绑定到 inputValue
值,onChange
监听输入框变化,然后将输入框的值更新到 inputValue
,最后显示出 inputValue
的值。