我正在将 Axios 与 React.js 结合使用。 我想在webservice响应后调用一个函数。但它不起作用,这是我的代码:
public onValidateRenammeNature = () => { // bouton valider, renomme la nature
let id
let newName
console.log("this.state.myNature.id : " + this.state.myNature.id)
id = this.state.myNature.id
newName = this.refs.nature.getValue()
axios.post('/UpdateNature', {
id: id,
name: newName
}).then(
//here I want to call this function after the webservice is executed, but I don't know how can I do that
this.getAllNaturesFromData()
)
this.setState({
openRenammeNature: false,
});
}
最佳答案
.then()
函数接受一个函数作为参数,你可以这样做
axios.post('/UpdateNature', {
id: id,
name: newName
}).then(function(response){
this.getAllNaturesFromData()
})
//ES6 Arrow function work as well
axios.post('/UpdateNature', {
id: id,
name: newName
}).then((response) =>{
this.getAllNaturesFromData()
})
关于reactjs - 轴 : call function after response,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46420656/