Axios is a promise-based lightweight HTTP client library based on the $http
service, which gives you the ability to take advantage of JavaScript’s async
and await
for more readable asynchronous code
This helps us make http requests to external resources in React applications we retrieve data from external API’s, after add data we add it to the state, so it’s ready to be used by our application
Integrating and configuring Axios
Using npm
$ npm install axios
Using bower:
$ bower install axios
Using yarn:
$ yarn add axios
Create axios.js file ( using ‘get’ Method)
impot axios from "axios";
// base url
const instance = axios.create({
baseURL: "http://api.product.org"
});
export default instance;
import axios from './axios'; async function fetchData() { const request = await axios.get('/trending/products') }
Also Use
axios.get('/trending/products')
.then(function(response) {
//success
})
.catch(function(error) {
//handleerror
})
.then(function() {
//always executed
});