Axios:响应后调用函数

Axios : call function after response

我在 React.js 中使用 Axios。 我想在 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()
        })

您需要向 .then 函数发送参数。

axios.post('/UpdateNature', {            
    id: id,
    name: newName
  }).then(function(res){
      if(res.statusCode === 200){
          this.getAllNaturesFromData();      // Your function call
      }
  })