How to use axios on a react js project

First install axios via npm by using the following code

npm install axios --save

Then add the following code to your react project

import axios from 'axios'

const promise = axios.get('http://localhost:3001/json_root')
console.log(promise)
//this console log prints the promise returned by axios

If you are expecting a result that you can later use somewhere then you need to attach an event handler to the promise that gets returned from axios

const promise = axios.get('http://localhost:3001/json_root')
promise.then(response => {
  console.log(response)
})

Leave a Comment

Your email address will not be published. Required fields are marked *