Search code examples
apireactjsjsfiddle

API call in a react JS fiddle


I am hoping for some direction on this issue. I have a project which must be done in a js fiddle. I am trying to make an API call and I want to use 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/


Solution

  • Add the superagent lib from cdnjs

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

    then initialize component's state

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

    and make the query on 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);
    },
    

    end result: https://jsfiddle.net/jperelli/p4svwhxp/3/