2017-09-26 15 views
1

私はReact.jsでAxiosを使用しています。 Webサービスの応答の後に関数を呼び出す必要があります。しかし、それは動作しません、これは私のコードです:.then()関数は、引数としての機能を取り込み、あなたの助けをありがとう:)Axios:応答後に関数を呼び出す

答えて

1

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,     
    }); 
} 

することは、あなたは

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() 
     }) 
+0

あなたは正しいが、それは私のために動作しません...私はあなたが取得している応答で他にどのような答え –

0

を行うことができます関数.thenに引数を送る必要があります。

axios.post('/UpdateNature', {    
    id: id, 
    name: newName 
    }).then(function(res){ 
     if(res.statusCode === 200){ 
      this.getAllNaturesFromData();  // Your function call 
     } 
    }) 
-1
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() 

     console.log("this.props.natureSelected : " + this.props.natureSelected) 
     axios.post('/UpdateNature', {    
      id: id, 
      name: newName 
     }).then((response) => { 
      console.log("onValidateRenammeNature then") 
//The code is executed, but the list isn't update .. 
      this.getAllNaturesFromData() 
     }) 


    } 
+0

に私のコードを編集した、何が持っているとされていません実行されましたか? –

+0

@JamesMaaあなたが正しいです、私は私の小道具でエラーがあった。今は仕事です。どうもありがとうございました ! –

関連する問題