【问题标题】:TypeError: Cannot read property 'info' of undefinedTypeError:无法读取未定义的属性“信息”
【发布时间】:2017-12-26 23:40:04
【问题描述】:

我对反应非常陌生,在将数据从一种方法传递到另一种方法时遇到问题。 这是我的反应语法:

var url = "https://quotesondesign.com/wp-json/posts?filter[orderby]=rand&filter[posts_per_page]=1"
class App extends React.Component{
  info(val){
  console.log(val)
  }

request(){
  axios.get(url)
  .then(function (response) {
      this.info(response)
    console.log(response.data);
  })
  }

render() {
    return(

    <div>
        <h1>axios</h1>
      {this.request()}
      </div>

    )
  }
}

ReactDOM.render(<App />, document.getElementById("target"))

我的目标是将响应数据从request 方法传递给info 方法。但是,我收到错误消息说"TypeError: Cannot read property 'info' of undefined" 你能帮我找出我缺少的东西吗?

【问题讨论】:

  • 绑定问题,检查重复问题

标签: javascript reactjs react-native axios


【解决方案1】:

非常常见的问题和许多相同的答案,所以添加答案作为社区 wiki。

这是一个绑定问题,您需要将this 与回调绑定。

使用arrow function:

.then( (response) => {

更多详情请查看此答案:Why is JavaScript bind() necessary?

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2020-11-29
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-02-17
    • 2018-10-09
    • 2018-11-14
    相关资源
    最近更新 更多