API 调用 React JS fiddle

API call in a react JS fiddle

我希望在这个问题上得到一些指导。我有一个项目必须在 js fiddle 中完成。我正在尝试拨打 API 电话,我想使用 superagent(http://visionmedia.github.io/superagent/) to make the get request, but I can't figure out how to get it to work in the fiddle. Does anyone have any suggestions for this issues, or perhaps a better way to approach the problem? I am new to react so any guidance would be appreciated. Here is a link to the fiddle enter code herehttps://jsfiddle.net/Sleahy115/69r7amhs/

从 cdnjs 添加 superagent 库

<script src="https://cdnjs.cloudflare.com/ajax/libs/superagent/0.15.7/superagent.min.js"></script>

然后初始化组件的状态

getInitialState () {
  return {
    repos: []
  };
},

并在 componentDidMount

上进行查询
componentDidMount: function() {
  var component = this;
  var url = 'https://api.github.com/users/coma/repos';
  var end = function (error, response) {
    component.setState({repos:response.body})
  };
  var req = superagent.get(url).end(end);
},

最终结果:https://jsfiddle.net/jperelli/p4svwhxp/3/