2
嘿傢伙試圖反應出來,有一個問題,當使用setState
,我一直得到無法讀取屬性setState
未定義的錯誤,我不知道如何解決它。我試過在構造函數中使用綁定,但仍不能解決問題。反應這是不明確的,當使用setState
感謝您的輸入。
import React from 'react';
class Products extends React.Component {
constructor(props) {
super(props)
this.state = {products:{}}
this.getItems = this.getItems.bind(this)
}
componentDidMount() {
this.getItems('http://lcboapi.com/products/300681').then(function(response){
console.log(response);
this.setState({products:response});
},function(error){
console.log('failed',error);
});
}
componentWillMount() {
}
getItems(url){
return new Promise(function (resolve,reject) {
var req = new XMLHttpRequest();
req.open('GET',url);
req.setRequestHeader('Authorization','Token Token');
req.onload = function(){
if(req.status == 200){
resolve(req.response);
}else{
reject(Error(req.statusText));
}
};
req.onerror = function(){
reject(Error("Error"));
};
req.send();
});
}
render() {
return (
<div>
hi
</div>
);
}
}
export default Products;
打我衝,upvote ;-) – lustoykov
哇,我整個晚上都錯了。箭頭功能將這種背景向前推進,我不知道這一點。將多一點潛入ES6!謝謝! –