我的代码有什么问题? 我有一个错误,当我从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