How to integrate a third-party library like axios in React?

To integrate a third-party library like axios in a React project, you can follow these steps:

  1. Install axios using npm or yarn:
npm install axios

or

yarn add axios
  1. Import axios in your React component where you want to make API calls:
import axios from 'axios';
  1. You can now use axios to make API calls in your component. Here is an example of making a GET request using axios:
axios.get('https://api.example.com/data') .then(response => { console.log(response.data); }) .catch(error => { console.log(error); });
  1. 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.

  2. 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.