我有一个类组件如下:
class App extends Component {
constructor(props){
super(props);
this.state = {
abc: '',
someQuery: ''
}
this.handleSubmit = this.handleSubmit.bind(this);
this.handleChange = this.handleChange.bind(this);
}
componentDidUpdate(){
fetch(`/someLink/${this.state.abc}`)
.then(response => {
return response.json();
}).then(data => {
this.setState({
someQuery: data.xxx
});
});
}
handleSubmit(e){
const target = e.target;
const value = target.value;
this.setState({
abc: value
})
e.preventDefault();
};
handleChange(e){
const target = e.target;
const value = target.value;
this.setState({
abc: value
});
};
render(){
return(
<form onSubmit={this.handleSubmit}>
<input name='abc' value={this.state.abc} onChange={this.handleChange} />
<input type="submit" value="Submit" />
</form>
<div>{this.state.abc} is currently accessing data from {this.state.someQuery}</div>
)
}
}
componentDidUpdate()
每次更新输入字段的值并单击提交按钮时如何运行?
上面调用了生命周期,但由于其中的原因setState
,handleChange()
生命周期在我输入内容的那一刻被调用,而不是等到单击提交按钮。
删除setState
fromhandleChange()
会使输入字段值不再可编辑(无法在输入字段上键入)。
我需要api link
在生命周期中附加输入字段值,但我似乎无法找出正确的方法来做到这一点。