reactjs - 轴 : call function after response

标签 reactjs axios

我正在将 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/

相关文章:

javascript - 合并来自链式 axios 请求的两个数组

vue.js - vue-typeahead 说你需要提供一个 HTTP 客户端

reactjs - ESLint - 强制 PropTypes

javascript - 使用 Connect 进行 mapStateToProps 和 mapDispatchToProps 时对 redux : still getting empty object for this. props 使用react

javascript - 如何使用 React js JSX 在下拉列表中创建年份列表

javascript - 为什么这个请求取消在 axios 中不起作用?

node.js - 从 get 请求中的函数获取返回值

reactjs - React PureComponent 中的 shouldComponentUpdate 实现

reactjs - 如何修复 css 文件上的 typescript 编译器错误?

node.js - 使用Ubuntu在远程服务器上部署nodejs应用程序