TypeError:无法读取未定义的属性“info”

时间:2017-07-21 04:15:29

标签: javascript reactjs react-native axios

我很反应,在将数据从一个方法传递到另一个方法时遇到了问题。 这是我的反应语法:

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" 你能帮我辨别一下我错过的东西吗?

1 个答案:

答案 0 :(得分:2)

非常常见的问题以及可用于此的许多答案,因此添加答案为社区维基。

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

使用arrow function

.then( (response) => {

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

相关问题