# Axios in React

Axios in React simplifies HTTP requests using a promise-based API for methods like `get`, `post`, `put`, `delete`, and `patch`, offering features like <mark class="HxTRcb" data-processed="true">automatic JSON transformation, request/response interception, cancellation, timeout settings, and built-in error handling</mark> (rejecting on 4xx/5xx status codes). Key properties include `response.data` for payload, `error.response` for detailed HTTP errors, `config` for settings (headers, timeouts), and `axios.create()` for custom instances, making API communication clean and efficient in React apps.<span class="uJ19be notranslate" data-processed="true" data-wiz-uids="ci3AU_o,ci3AU_p,ci3AU_q"><span class="vKEkVd" data-animation-atomic="" data-processed="true"> </span></span>

<div class="Y3BBE" data-hveid="CAEQAA" data-processed="true" data-sfc-cp="" id="bkmrk-" jsaction="rcuQ6b:&ci3AU_8|npT2md" jscontroller="zcfIf" jsuid="ci3AU_8">  
</div><div class="Y3BBE" data-hveid="CAIQAA" data-processed="true" data-sfc-cp="" id="bkmrk-this-video-provides-" jsaction="rcuQ6b:&ci3AU_w|npT2md" jscontroller="zcfIf" jsuid="ci3AU_w">This video provides a tutorial on making API requests with Axios in React: <div class="" data-processed="true" data-wiz-uids="ci3AU_y,ci3AU_z" jscontroller="uDeH3" jsuid="ci3AU_x"><div data-hveid="CAIQAQ" data-processed="true" data-ved="2ahUKEwjx_KHqn7CRAxU0SGwGHaCRLPIQ-bUQegQIAhAB"><div data-processed="true"><div class="Q2WBBe" data-processed="true" jsuid="ci3AU_z"><div aria-label="" class="r0hyfc" data-animation-nesting="" data-processed="true" data-ved="2ahUKEwjx_KHqn7CRAxU0SGwGHaCRLPIQypYSegQIAhAC" jsuid="ci3AU_y" tabindex="-1"><div class="RtAO3b" data-processed="true" data-sfc-cp="" data-wiz-uids="ci3AU_11" jsaction="rcuQ6b:&ci3AU_10|npT2md" jscallback="gfNXHe:&ci3AU_x:U8wGUb" jscontroller="wMmv4c" jsuid="ci3AU_10"><div class="nrdPJf" data-processed="true" data-ved="2ahUKEwjx_KHqn7CRAxU0SGwGHaCRLPIQtbEQegQIAhAD" jsaction="click:&ci3AU_10|T2P31d" jsuid="ci3AU_11" role="button" tabindex="0"><div class="n7j80c YkW4Kb" data-processed="true" jscontroller="ItMuwb" jsuid="ci3AU_12" style="--segment-bar-offset: 12px;"><div class="llCCib" data-processed="true" style="left: 25%;"><svg fill="currentColor" height="12" viewbox="0 0 12 12" width="12" xmlns="http://www.w3.org/2000/svg"></svg></div></div></div></div></div></div></div></div></div></div><div class="n7j80c YkW4Kb" data-processed="true" id="bkmrk--1" jscontroller="ItMuwb" jsuid="ci3AU_12" style="--segment-bar-offset: 12px;"></div><div class="dumoDb YkW4Kb" data-processed="true" id="bkmrk--2"><div class="CaQWce" data-processed="true" jscontroller="sRLmTc" jsuid="ci3AU_13"><svg fill="currentColor" height="18px" viewbox="0 -960 960 960" width="18px" xmlns="http://www.w3.org/2000/svg"></svg></div></div><div class="Y3BBE" data-hveid="CAIQAA" data-processed="true" data-sfc-cp="" id="bkmrk-dave-gray-youtube-%E2%80%A2-" jsaction="rcuQ6b:&ci3AU_w|npT2md" jscontroller="zcfIf" jsuid="ci3AU_w"><div class="" data-processed="true" data-wiz-uids="ci3AU_y,ci3AU_z" jscontroller="uDeH3" jsuid="ci3AU_x"><div data-hveid="CAIQAQ" data-processed="true" data-ved="2ahUKEwjx_KHqn7CRAxU0SGwGHaCRLPIQ-bUQegQIAhAB"><div data-processed="true"><div class="Q2WBBe" data-processed="true" jsuid="ci3AU_z"><div aria-label="" class="r0hyfc" data-animation-nesting="" data-processed="true" data-ved="2ahUKEwjx_KHqn7CRAxU0SGwGHaCRLPIQypYSegQIAhAC" jsuid="ci3AU_y" tabindex="-1"><div class="RtAO3b" data-processed="true" data-sfc-cp="" data-wiz-uids="ci3AU_11" jsaction="rcuQ6b:&ci3AU_10|npT2md" jscallback="gfNXHe:&ci3AU_x:U8wGUb" jscontroller="wMmv4c" jsuid="ci3AU_10"><div class="nrdPJf" data-processed="true" data-ved="2ahUKEwjx_KHqn7CRAxU0SGwGHaCRLPIQtbEQegQIAhAD" jsaction="click:&ci3AU_10|T2P31d" jsuid="ci3AU_11" role="button" tabindex="0"><div class="dumoDb YkW4Kb" data-processed="true">  
</div></div></div></div><div data-processed="true">[<div class="RhEuY" data-processed="true"><div class="SWvopd" data-processed="true" jscontroller="aNJZAb" jsuid="ci3AU_14"><div class="Lki2rc" data-processed="true"><div aria-hidden="true" class="U9BD8 Wsaimf QyEYne" data-processed="true" jscontroller="Cky8Oc" jsuid="ci3AU_15">  
</div></div><div class="JccCTc xG6cCf" data-processed="true"><div aria-hidden="true" class="U9BD8 Wsaimf QyEYne" data-processed="true" jscontroller="Cky8Oc" jsuid="ci3AU_16">  
</div></div></div><div data-processed="true"><div class="SrjfCd" data-processed="true">Dave Gray</div><div class="Pcl3Vd" data-processed="true">YouTube • 3 Aug 2021</div></div></div>](https://www.youtube.com/watch?v=ZEKBDXGnD4s&t=549)</div></div></div></div></div></div><div class="Fsg96" data-processed="true" data-sfc-cp="" id="bkmrk--3" jsaction="rcuQ6b:&ci3AU_17|npT2md" jscontroller="KHhJQ" jsuid="ci3AU_17">  
</div><div aria-level="3" class="otQkpb" data-animation-nesting="" data-processed="true" data-sfc-cp="" id="bkmrk-key-methods" jscontroller="a7qCn" jsuid="ci3AU_18" role="heading">**Key Methods**</div><div aria-level="3" class="otQkpb" data-animation-nesting="" data-processed="true" data-sfc-cp="" id="bkmrk--4" jscontroller="a7qCn" jsuid="ci3AU_18" role="heading">  
</div>- <span class="T286Pc" data-processed="true" data-sfc-cp="">**`axios(config)`**: The base method, takes a config object for any request type.</span>
- <span class="T286Pc" data-processed="true" data-sfc-cp="">**`axios.get(url, config)`**: Fetches data from a URL.</span>
- <span class="T286Pc" data-processed="true" data-sfc-cp="">**`axios.post(url, data, config)`**: Sends data to create a resource.</span>
- <span class="T286Pc" data-processed="true" data-sfc-cp="">**`axios.put(url, data, config)`**: Updates an existing resource.</span>
- <span class="T286Pc" data-processed="true" data-sfc-cp="">**`axios.delete(url, config)`**: Deletes a resource.</span>
- <span class="T286Pc" data-processed="true" data-sfc-cp="">**`axios.patch(url, data, config)`**: Partially updates a resource.</span>
- <span class="T286Pc" data-processed="true" data-sfc-cp="">**`axios.head(url, config)`**: Gets headers without response body.</span>
- <span class="T286Pc" data-processed="true" data-sfc-cp="">**`axios.options(url, config)`**: Checks server options.</span><span class="uJ19be notranslate" data-processed="true" data-wiz-uids="ci3AU_29,ci3AU_2a,ci3AU_2b"><span class="vKEkVd" data-animation-atomic="" data-processed="true"> </span></span>

<div class="Fsg96" data-processed="true" data-sfc-cp="" id="bkmrk--5" jsaction="rcuQ6b:&ci3AU_2d|npT2md" jscontroller="KHhJQ" jsuid="ci3AU_2d">  
</div><div aria-level="3" class="otQkpb" data-animation-nesting="" data-processed="true" data-sfc-cp="" id="bkmrk-core-properties-%26-fe" jscontroller="a7qCn" jsuid="ci3AU_2e" role="heading">**Core Properties &amp; Features**</div><div aria-level="3" class="otQkpb" data-animation-nesting="" data-processed="true" data-sfc-cp="" id="bkmrk--6" jscontroller="a7qCn" jsuid="ci3AU_2e" role="heading">  
</div>- <span class="T286Pc" data-processed="true" data-sfc-cp="">**Promises**: Handles async operations with `.then()` (success) and `.catch()` (error).</span>
- <span class="T286Pc" data-processed="true" data-sfc-cp="">**Automatic JSON**: Converts JSON responses to JavaScript objects automatically.</span>
- <span class="T286Pc" data-processed="true" data-sfc-cp="">**Interceptors**: Modify requests or responses globally before handling (e.g., adding auth tokens).</span>
- <span class="T286Pc" data-processed="true" data-sfc-cp="">**Error Handling**: Throws errors for non-2xx status codes (404, 500), unlike Fetch.</span>
- <span class="T286Pc" data-processed="true" data-sfc-cp="">**Timeouts**: Set `timeout` in config to prevent hanging requests.</span>
- <span class="T286Pc" data-processed="true" data-sfc-cp="">**Cancellation**: Cancel ongoing requests using `CancelToken` or `AbortController`.</span>
- <span class="T286Pc" data-processed="true" data-sfc-cp="">**`axios.create()`**: Creates reusable instances with default configs (base URL, headers).</span><span class="uJ19be notranslate" data-processed="true" data-wiz-uids="ci3AU_3a,ci3AU_3b,ci3AU_3c"><span class="vKEkVd" data-animation-atomic="" data-processed="true"> </span></span>

<div class="Y3BBE" data-hveid="CAcQAA" data-processed="true" data-sfc-cp="" id="bkmrk-this-video-explains-" jsaction="rcuQ6b:&ci3AU_3j|npT2md" jscontroller="zcfIf" jsuid="ci3AU_3j">This video explains how to create custom Axios hooks for your React application: <div class="" data-processed="true" data-wiz-uids="ci3AU_3l,ci3AU_3m" jscontroller="uDeH3" jsuid="ci3AU_3k"><div data-hveid="CAcQAQ" data-processed="true" data-ved="2ahUKEwjx_KHqn7CRAxU0SGwGHaCRLPIQ-bUQegQIBxAB"><div data-processed="true"><div class="Q2WBBe" data-processed="true" jsuid="ci3AU_3m"><div aria-label="" class="r0hyfc" data-animation-nesting="" data-processed="true" data-ved="2ahUKEwjx_KHqn7CRAxU0SGwGHaCRLPIQypYSegQIBxAC" jsuid="ci3AU_3l" tabindex="-1"><div class="RtAO3b" data-processed="true" data-sfc-cp="" data-wiz-uids="ci3AU_3o" jsaction="rcuQ6b:&ci3AU_3n|npT2md" jscallback="gfNXHe:&ci3AU_3k:U8wGUb" jscontroller="wMmv4c" jsuid="ci3AU_3n"><div class="nrdPJf" data-processed="true" data-ved="2ahUKEwjx_KHqn7CRAxU0SGwGHaCRLPIQtbEQegQIBxAD" jsaction="click:&ci3AU_3n|T2P31d" jsuid="ci3AU_3o" role="button" tabindex="0"><div class="n7j80c YkW4Kb" data-processed="true" jscontroller="ItMuwb" jsuid="ci3AU_3p" style="--segment-bar-offset: 12px;"><div class="llCCib" data-processed="true" style="left: 20%;"><svg fill="currentColor" height="12" viewbox="0 0 12 12" width="12" xmlns="http://www.w3.org/2000/svg"></svg></div></div></div></div></div></div></div></div></div></div><div class="n7j80c YkW4Kb" data-processed="true" id="bkmrk--7" jscontroller="ItMuwb" jsuid="ci3AU_3p" style="--segment-bar-offset: 12px;"></div><div class="Y3BBE" data-hveid="CAcQAA" data-processed="true" data-sfc-cp="" id="bkmrk-code-radiance-youtub" jsaction="rcuQ6b:&ci3AU_3j|npT2md" jscontroller="zcfIf" jsuid="ci3AU_3j"><div class="" data-processed="true" data-wiz-uids="ci3AU_3l,ci3AU_3m" jscontroller="uDeH3" jsuid="ci3AU_3k"><div data-hveid="CAcQAQ" data-processed="true" data-ved="2ahUKEwjx_KHqn7CRAxU0SGwGHaCRLPIQ-bUQegQIBxAB"><div data-processed="true"><div class="Q2WBBe" data-processed="true" jsuid="ci3AU_3m"><div aria-label="" class="r0hyfc" data-animation-nesting="" data-processed="true" data-ved="2ahUKEwjx_KHqn7CRAxU0SGwGHaCRLPIQypYSegQIBxAC" jsuid="ci3AU_3l" tabindex="-1"><div class="RtAO3b" data-processed="true" data-sfc-cp="" data-wiz-uids="ci3AU_3o" jsaction="rcuQ6b:&ci3AU_3n|npT2md" jscallback="gfNXHe:&ci3AU_3k:U8wGUb" jscontroller="wMmv4c" jsuid="ci3AU_3n"><div class="nrdPJf" data-processed="true" data-ved="2ahUKEwjx_KHqn7CRAxU0SGwGHaCRLPIQtbEQegQIBxAD" jsaction="click:&ci3AU_3n|T2P31d" jsuid="ci3AU_3o" role="button" tabindex="0"><div class="dumoDb YkW4Kb" data-processed="true">  
</div></div></div></div><div data-processed="true">[<div class="RhEuY" data-processed="true"><div class="SWvopd" data-processed="true" jscontroller="aNJZAb" jsuid="ci3AU_3r"><div class="Lki2rc" data-processed="true"><div aria-hidden="true" class="U9BD8 Wsaimf QyEYne" data-processed="true" jscontroller="Cky8Oc" jsuid="ci3AU_3s">  
</div></div><div class="JccCTc xG6cCf" data-processed="true"><div aria-hidden="true" class="U9BD8 Wsaimf QyEYne" data-processed="true" jscontroller="Cky8Oc" jsuid="ci3AU_3t">  
</div></div></div><div data-processed="true"><div class="SrjfCd" data-processed="true">Code Radiance</div><div class="Pcl3Vd" data-processed="true">YouTube • 11 Feb 2024</div></div></div>](https://www.youtube.com/watch?v=WtijdoNfzYg&t=166)</div></div></div></div></div></div><div class="Fsg96" data-processed="true" data-sfc-cp="" id="bkmrk--8" jsaction="rcuQ6b:&ci3AU_3u|npT2md" jscontroller="KHhJQ" jsuid="ci3AU_3u">  
</div><div aria-level="3" class="otQkpb" data-animation-nesting="" data-processed="true" data-sfc-cp="" id="bkmrk-common-react-usage-p" jscontroller="a7qCn" jsuid="ci3AU_5b" role="heading">Common React Usage Pattern</div><div aria-level="3" class="otQkpb" data-animation-nesting="" data-processed="true" data-sfc-cp="" id="bkmrk--9" jscontroller="a7qCn" jsuid="ci3AU_5b" role="heading">  
</div><div class="Fsg96" data-processed="true" data-sfc-cp="" id="bkmrk--10" jsaction="rcuQ6b:&ci3AU_5f|npT2md" jscontroller="KHhJQ" jsuid="ci3AU_5f">  
</div><div class="r1PmQe" data-hveid="CAsQAA" data-processed="true" data-wiz-uids="ci3AU_64,ci3AU_65,ci3AU_66" id="bkmrk-javascript" jscontroller="HP6Sjf" jsuid="ci3AU_63"><div data-processed="true"><div class="pHpOfb" data-animation-atomic="" data-processed="true"><div class="vVRw1d" data-processed="true">javascript</div></div></div></div>```
import axios from 'axios';
import React, { useEffect, useState } from 'react';

function MyComponent() {
  const [data, setData] = useState(null);

  useEffect(() => {
    axios.get('https://api.example.com/items') // .get(url, { params: {} }) for query params
      .then(response => {
        setData(response.data); // Access data here
      })
      .catch(error => {
        console.error('Error fetching data:', error); // Detailed error handling
      });
  }, []);

  return (
    <div>
      {data ? {JSON.stringify(data, null, 2)} : <p>Loading...</p>}
    </div>
  );
}

```