To integrate a third-party library like axios in a React project, you can follow these steps:
npm install axios
or
yarn add axios
import axios from 'axios';
axios.get('https://api.example.com/data')
.then(response => {
console.log(response.data);
})
.catch(error => {
console.log(error);
});
You can also create a separate service file to handle all your API requests. Create a new file, for example api.js
, and define your API calls there. Then import and use this service file in your React components.
You can also set default configurations for axios, such as setting a base URL or default headers. For example:
axios.defaults.baseURL = 'https://api.example.com';
axios.defaults.headers.common['Authorization'] = 'Bearer token';
By following these steps, you can easily integrate a third-party library like axios in your React project to handle API calls.