Introduction
In the previous article, we learned about the concept of Render Props and Context in React. In this article, we will dive into some advanced concepts of React, like HTTP and usage of the GET and POST methods for data retrieval.
Basics of HTTP and React
So far, we have learned about the basics of React and how props and state are used. Now, we will learn how React makes API calls fetch data and display in the browser. As React is just a library for the user interface, it only knows about Props and State so here, HTTP comes into the picture.
To fetch data in React, there are many HTTP libraries available like - Apollo, Axios, Relay Modern, Request, and Superagent. In this article, we are going to use Axios library.
First of all, we will create a new React app using the following command.
- npx create-react-app http-react
The app is created now. Now, go to your newly created folder and install Axios to use it further in our application.
- npm install axios
After the command is executed successfully, we will see in the package.json file that the dependencies are updated.

Now, we will see how we can fetch data and display in the browser.
HTTP GET
Here, we are using an online API to fetch data from it instead of creating our own API. We are using https://reqres.in/ to test our application.
Create a component named DisplayList.js.
- import React, { Component } from 'react'
- export class DisplayList extends Component {
- render() {
- return (
- <div>
- List of Users
- </div>
- )
- }
- }
- export default DisplayList
And include this component in App.js.
- import React from 'react';
- import logo from './logo.svg';
- import './App.css';
- import DisplayList from './components/DisplayList'
- function App() {
- return (
- <div className="App">
- <DisplayList></DisplayList>
- </div>
- );
- }
- export default App;
It will display the output as below.

Now, we will fetch data from library using Axios. For that, first, we need to import the axios library.
- import axios from 'axios'
Now, to store the data that we get from API, we will create an empty array in state.
- this.state = {
- Users:[]
- }
Now, we will call the API and store the data in our array in componentDidMount() lifecycle method. This is executed when the component mounts for the first time and it is called only once.
So, the component will go like below.
- import React, { Component } from 'react'
- import axios from 'axios'
- const error ={
- color: 'red',
- fontWeight:'bold',
- fontSize:'14'
- }
- export class DisplayList extends Component {
- constructor(props) {
- super(props)
- this.state = {
- users:[],
- errorMessage:""
- }
- }
- componentDidMount(){
- axios.get('https://reqres.in/api/users/')
- .then(response => {
- console.log(response.data.data)
- this.setState({
- users:response.data.data
- })
- })
- .catch(error =>{
- this.setState({
- errorMessage:"Error fetching data"
- })
- })
- }
- render() {
- const {users,errorMessage} = this.state
- console.log(users.length)
- return (
- <div>
- List of Users
- {
- users.length?
- users.map(user=> <div key={user.id}><img src={user.avatar} alt={user.first_name}/>{user.first_name + " "+ user.last_name}</div>):
- null
- }
- {errorMessage ? <div style={error}>{errorMessage}</div> : null}
- </div>
- )
- }
- }
- export default DisplayList




Neelesh MalviPosted Mar 10, 2023, 8:03 AM
Hi Priyanka, did you already published tutorial on advanced React?