我的代碼有什麼問題? 我有一個錯誤,當我從API獲取JSON:reactjs setState不是JSON調用的函數
this.setState不是一個函數
代碼:
class App extends React.Component {
constructor(props) {
super(props);
this.state = {
data: []
}
};
componentWillMount() {
const url = 'myurl';
http.get(url, function(res){
var body = '';
res.on('data', function(chunk){
body += chunk;
});
res.on('end', function(){
var res = JSON.parse(body);
console.log(res);
this.setState({data: res});
}.bind(this));
}).on('error', function(e){
console.log("Got an error: ", e);
});
}
}
提示#1:'的console.log(本);' – zerkms