Better Programming

Advice for programmers.

Follow publication

Member-only story

How to Use Async/Await with Axios in React

Aditya Singh
Better Programming
Published in
1 min readOct 25, 2018

Hello! I am a front end developer at Morfsys and I enjoy writing about things related to web, technology and more. In this piece, I’ll be covering how to best use async/await with Axios in React.

Installing Axios

First thing first, to get started, we’ll need to install Axios:

npm install --save axios

Making a Get Request

Next, let’s try to make a simple get request using Axios from our react component:

The above code makes a get request to https://reqres.in to retrieve a few users from their API and displays the user’s first name in our component.

Using Async/Await

Now, we want to make the same request using async/await.

Let's do it!

For a function to use await, we must wrap the function itself as an async function:

Create an account to read the full story.

The author made this story available to Medium members only.
If you’re new to Medium, create a new account to read this story on us.

Or, continue in mobile web

Already have an account? Sign in

Aditya Singh
Aditya Singh

Written by Aditya Singh

Javascript develover | Coffee | 420 | Beer

Write a response