This video is available to students only

Setting up Axios

In this lesson, we're going to set up axios in our project

Setting up axios#

We did work with axios in the beginning, but I want to set it up correctly. It's better to locate it at a different directory and then use it from different components, so let's do just that.

Inside the src directory, let's create a new folder called actions. Inside the actions folder, we need to make a file called agent.ts. This file will be responsible for all the API requests we make to our server.

First thing we need to do is import axios from axios. Also, in axios, we can store base url in a variable, so it gets easier if we want to append anything on it. We already know what our base url is, we just need to write:

We can also save our responseBody in a variable. If you remember, our data is stored inside response.data, so we do just that to make our lives a little bit easier.

Start a new discussion. All notification go to the author.