2

We have made a simple application that supports CRUD operations on a student model(Get Student by ID, Delete Student by ID etc). Can someone help with how to make a simple restful api call to the endpoint(http://ip:port/allstudents) and render the obtained json format in React UI? My code is as follows:

import React, { Component } from 'react';
import './App.css';
class App extends Component {
Getfunction() {
<a href="http://www.stackoverflow.com/">
<button>b1</button>
</a>
}
render() {
return (
<div>
  <button id='b1'
    style={{fontSize: 20, color: 'green'}}
    onClick={() => this.Getfunction()}>
    Get!
  </button>
);
}
}

export default App;

Where can I put my restful api call to query and how exactly?

4
  • Just google it. You can use fetch for Rest API Commented Mar 30, 2017 at 5:03
  • I have done that before posting the question here. But the examples were not clear enough.I have edited my question to be more precise! Commented Mar 30, 2017 at 5:06
  • You might want to look at how to ask good questions React is a library for building user interfaces, And hence you don't use 'react' to do computation and stuff like network calls. Feel free to use functionalities such as async/await or use fetch to collect data from a url. Look at the documentation for fetch developer.mozilla.org/en-US/docs/Web/API/Fetch_API/Using_Fetch. Commented Mar 30, 2017 at 5:07
  • 1
    This is such a common problem (that I and many of us have struggled with when working with CRUD apis) that I created a library to try to simplify this for all of us: github.com/DigitalGlobe/jetset Commented May 16, 2017 at 19:19

1 Answer 1

3

I would suggest you to make api call in life cycle method componentDidMount and when api call returns some data set it to your state and let your user interface update accordingly.

E.g your api call method may be like this -

ApiCall() {
    return $.ajax({
       url: 'http://ip:port/allstudents',      
       type: 'GET',
    }).fail((responseData) => {
        if (responseData.responseCode) {
            console.error(responseData.responseCode);
        }
    });
}

After new data is fetched from server set the state with new data

this.setState({data:newDataFromServer});

Basically above call will return you a jquery promise which you can use later.

Now in what ever method you want to make ApiCall just use like this -

class App extends Component {

 constructor() {
   this.state = {
      data : []
   }
 }
 componentDidMount()
 {
   this.getFunction();
 }

 getFunction = () => {
     this.ApiCall()
        .then(
            function(data){
              console.log(data);
              // set the state here
              this.setState({data:data});
            },
            function(error){
              console.log(error);
           }
    );
 }

 render() {
   return (
      <div>
        <button id='b1'
           style={{fontSize: 20, color: 'green'}}
           onClick={() => this.Getfunction()}>
        Get!
     </button>
     {/* Now render your data here using jsx if it is array iterate over it */}
    </div>
   );
 }
}
Sign up to request clarification or add additional context in comments.

1 Comment

Thanks! Will try this out

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.