Using Axios With React

Jessica Watts
2 min readMay 17, 2021

So what is Axios ?

Axios is a library that helps us make http requests to external resources.

Axios is promise-based giving the ability to use JavaScript and async await for more readable asynchronous code. it is used more often that fetch because it has a larger set of features and supports older browsers.

To install Axios follow the npm command:

Making a get() request:

First you will need to import axios from 'axios' in the component. Then you hook into the componentDidMount lifecycle hook and perform agetrequest.

You use axios.get(url) method and pass in the URL where the resource lives. Axios does its work and returns back a promise.

If the promise is successful, it’s passed to the then() method where we use it to set state.

If the promise is not successful we get back an error that is passed to the catch() method where we can display it on the console or in some other fashion.

--

--