2017-07-21 19 views
0

我是非常新的反應,當涉及到將數據從一個方法傳遞到另一個方法時,我遇到了問題。 這裏是我的反應語法:TypeError:無法讀取未定義的屬性'信息'

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" 你能幫我找出我失蹤的東西嗎?

+0

綁定問題,檢查重複的問題 –

回答

3

非常常見的問題和很多答案都可以提供相同的答案,所以添加的答案與社區wiki相同。

這是一個綁定問題,您需要綁定this與回調。

使用arrow function

.then((response) => { 

有關詳情,請這樣的回答:Why is JavaScript bind() necessary?

相關問題