Simplest way to use axios to fetch data from an api in ReactJS

Manish Mandal
How To React
Published in
4 min readJul 9, 2019

In this tutorial, I will show you how to fetch data from an API to your react app. It doesn’t matter what backend technology you are using (i.e Nodejs, WordPress, etc) you can fetch data from the backend using API and axios to your react app. I won’t explain what is an API, What is Axios because you can google it 😁 😁. So let us start the tutorial.

Requirements

  1. First, we need to create a react app. You can follow my previous tutorial Getting Started with ReactJS to create a simple hello world app.
  2. After that open your terminal and install axios
npm install axios --save

3. Now we will create an instance for baseURL. This instance is created so that we don’t have to manually change the base URl of the API in each component. So to create the instance we need to go our app src folder and create a file name axios.js

4. Now open your project inside your favorite IDE and paste this inside your newly created axios.js file

import axios from 'axios';const instance = axios.create({
baseURL: 'https://jsonplaceholder.typicode.com/'
});
export default instance;

Note: change https://jsonplaceholder.typicode.com/ with your own API base URL.

5. Now we will create our components folder to put all our components in one place. Now in your src folder create a new folder name components and inside that create another folder name users.

6. Now create a file users.js inside your users folder and create the react class component by pasting this

import React, { Component } from 'react'export default class users extends Component {
render() {
return (
<div>

</div>
)
}
}

7. Now import your users.js file in your app.js file inside your src folder.

8. Now import axios from your newly created axios.js file from your src folder to the component.

import axios from '../../axios'

9. Now it’s time to add a class constructor and assigns the initial state Users : []

constructor(props) {
super(props);
this.state = {
Users: []
};
}

10. Now we will create a function to call our users data from an API using axios to our component. Create a function name getUsersData after your class constructor and paste this inside your function

axios
.get(`/users`, {})
.then(res => {
const data = res.data
console.log(data)
})
.catch((error) => {
console.log(error)
})

Note: you need to call getUsersData function inside your componentDidMount lifecycle method by insertingthis.getUsersData()

11. Your users.js will look something like this.

12. Now run your app by typing npm startin your terminal and check the browser console. You will see an array of objects inside your console.

Hola 😎 we have successfully called our data from an API to our react app using axios.

13. Now it’s time to print the data in our app. For this, we will use the javascript map and arrow function to loop through our array of objects.

14. Just after your console.log(data) paste this

const users = data.map(u =>
<div>
<p>{u.id}</p>
<p>{u.name}</p>
<p>{u.email}</p>
<p>{u.website}</p>
<p>{u.company.name}</p>
</div>
)
this.setState({users})

15. Now call your users data using this.state.users inside return to print in your app

{this.state.users}

16. Finally, your users.js file will look like this.

Refresh your browser and tada 😱 all your data is printed.

Thanks for reading. Here is the live example and GIT repository for that.

--

--

Manish Mandal
How To React

Full Stack Developer | React JS | Next JS | Node JS | Vue JS | Wordpress. Connect with me https://www.linkedin.com/in/manishmandal21/