Author -  Sai gowtham

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

Css Tutorials & Demos

How rotate an image continuously in CSS

In this demo, we are going to learn about how to rotate an image continuously using the css animations.

How to create a Instagram login Page

In this demo, i will show you how to create a instagram login page using html and css.

How to create a pulse animation in CSS

In this demo, i will show you how to create a pulse animation using css.

Creating a snowfall animation using css and JavaScript

In this demo, i will show you how to create a snow fall animation using css and JavaScript.

Top Udemy Courses

JavaScript - The Complete Guide 2023 (Beginner + Advanced)
JavaScript - The Complete Guide 2023 (Beginner + Advanced)
116,648 students enrolled
52 hours of video content
$14.99 FROM UDEMY
React - The Complete Guide (incl Hooks, React Router, Redux)
React - The Complete Guide (incl Hooks, React Router, Redux)
631,582 students enrolled
49 hours of video content
$24.99 FROM UDEMY
Vue - The Complete Guide (w/ Router, Vuex, Composition API)
Vue - The Complete Guide (w/ Router, Vuex, Composition API)
203,937 students enrolled
31.5 hours of video content
$14.99 FROM UDEMY