Q1. What is Axios?
Axios is a promise-based HTTP client for the browser and Node.js. It provides a simple API for making requests, supports request/response interception, and automatically transforms JSON data.
Q2. How do you use Axios in React?
Install axios, then import it. Example: useEffect(() => { axios.get('/api/data').then(response => setData(response.data)).catch(err => setError(err)); }, []);. Axios returns the data in response.data.
Q3. What are the advantages of Axios over fetch?
Axios has a cleaner syntax, automatic JSON transformation, request/response interceptors, and better error handling (e.g., catches HTTP errors). It also supports request cancellation and progress tracking.
Q4. How do you handle errors with Axios?
In a catch block, you can check error.response for server response, error.request for no response, or error.message. You can also use interceptors to globally handle errors.
Q5. Can you cancel an Axios request?
Yes, using CancelToken or AbortController (in newer versions). Create a source, pass the token to the request, and call source.cancel() in cleanup.
