onSubmit = (e) => {
var responseNum;
axios.get("/offeringmemo/settings")
.then(response => {
responseNum = response.data.cell_number
console.log(responseNum)
})
.catch(error => {
console.log(error)
})
this.setState({
to_number: responseNum
});
}
当我在控制台日志中记录“ response.data.cell_number”时,它记录正确的响应,但是在控制台在设置状态后记录this.state.to_number时,状态保持不变。不知道这是否与我如何访问此内容有关。
更新:
onSubmit = (e) => {
axios.get("/offeringmemo/settings")
.then(response => {
this.setState( {
to_number: response.data.cell_number
})
})
.catch(error => {
console.log(error)
})
console.log(this.state.to_number)
}
控制台记录“未定义”。