How to make http requests in react

by Sai gowtham2min read
How to make http requests in react

In this tutorial, we are going to learn about how to send Ajax requests in react or how to fetch the data from APIs.

We are using axios http library to fetch the data from the backend.

Axios: its a Promise based HTTP client for the browser and node.js.

first, we need to install the axios library by using the npm package manager.

npm i axios

once you successfully installed the axios let’s make some api calls to the server.

import React,{Component} from 'react'
import axios from 'axios';

class App extends Component{

componentDidMount(){
  axios.get('https://jsonplaceholder.typicode.com/todos/1')
  .then(res=>console.log(res))
  .catch(err=>console.log(err))
}


    render(){
        return (
            <div>
            <h1>Http requests in react</h1>
            </div>
        )
    }

}

In the above code, we make the http request inside the componentDidMount() life cycle method and log the response in the console.

Why componentDidMount ?

In the react componentDidMount lifecycle method is invoked immediately after a component is connected to the browsers dom.so that it is the correct place to make the http requests.

Let’s store the data inside the state instead of logging in the browser.

Full example code

import React,{Component} from 'react'
import axios from 'axios';

class App extends Component{

state = {
   todo:null
}


componentDidMount(){
  axios.get('https://jsonplaceholder.typicode.com/todos/1')
  .then(res=>{
      this.setState({
          todo:res.data
      })
  })
  .catch(err=>console.log(err))
}


    render(){
        return (
            <div>
            <h1>Http requests in react</h1>
            {this.state.todo ? <p>{this.state.todo.title}</p> : <p>Loading...</p>}
            </div>
        )
    }

}

We are showing the Loading... message to the user if the request is still in process.

Codepen demo