| ์ผ | ์ | ํ | ์ | ๋ชฉ | ๊ธ | ํ |
|---|---|---|---|---|---|---|
| 1 | 2 | 3 | 4 | |||
| 5 | 6 | 7 | 8 | 9 | 10 | 11 |
| 12 | 13 | 14 | 15 | 16 | 17 | 18 |
| 19 | 20 | 21 | 22 | 23 | 24 | 25 |
| 26 | 27 | 28 | 29 | 30 | 31 |
- ๋ชจ๋์นํฐํธ
- borderstyle
- ํฐํธ์จ์ดํธ
- selectorslevel4
- captionside
- textshadow
- ์์ด๋์ ๋ ํฐ
- ๋นํ๊ทธ
- fontweight
- liststyleimage
- liststyletype
- texttransform
- inlineblock
- liststyleposition
- formtag
- ์ฃผ์
- bordercollapse
- wordspacing
- CDN
- ํด๋์ค์ ๋ ํฐ
- gtammertable
- textdecoration
- ํผํ๊ทธ
- ๊ทธ๋ฃน์ ๋ ํฐ
- ํ์ ์ ๋ ํฐ
- borderspacing
- ์ ๋๋ฒ์ ์ ๋ ํฐ
- ํฅ์ค์ฝ๋
- ๊ตฌ๊ธํฐํธ
- ์ถ์ฝ
- Today
- Total
็ก์
Axios (์ก์์ค์ค) ๋ณธ๋ฌธ
๐ก Axios
์น ๋ธ๋ผ์ฐ์ ์ Node.js์์ HTTP ์์ฒญ์ ๋ณด๋ผ ์ ์๋ Promise ๊ธฐ๋ฐ์ ๋ผ์ด๋ธ๋ฌ๋ฆฌ.
โผ๏ธ ์ฃผ์ ํน์ง
- GET, POST, PUT, PATCH, DELETE ๋ฑ ๋ค์ํ HTTP ๋ฉ์๋ ์ง์
- Promise ๊ธฐ๋ฐ ๋น๋๊ธฐ ์ฒ๋ฆฌ
- ์์ฒญ(Request)๊ณผ ์๋ต(Response) ๊ฐ๋ก์ฑ๊ธฐ(Interceptor) ์ง์
- ์์ฒญ ๋ฐ ์๋ต ๋ฐ์ดํฐ๋ฅผ ์๋์ผ๋ก JSON ๋ณํ
- ์์ฒญ ์ทจ์ ๋ฐ ํ์์์ ์ค์ ์ง์
โผ๏ธ ์ฌ์ฉ ๋ฐฉ๋ฒ
import axios from 'axios';
// ์๋ฒ์์ ์ํ ๋ชฉ๋ก ๋ฐ์ดํฐ๋ฅผ ๊ฐ์ ธ์ค๋ ์์
axios.get('https://api.example.com/products')
.then(response => {
// ์์ฒญ์ด ์ฑ๊ณตํ์ ๋ ์คํ๋๋ ์ฝ๋
console.log(response.data); // ์๋ฒ์์ ๋ฐ์ ๋ฐ์ดํฐ
})
.catch(error => {
// ์์ฒญ์ด ์คํจํ์ ๋ ์คํ๋๋ ์ฝ๋
console.error('๋ฐ์ดํฐ๋ฅผ ๊ฐ์ ธ์ค๋ ์ค ์ค๋ฅ ๋ฐ์:', error);
});
- ๋์ ์์:
- axios.get()์ผ๋ก ์๋ฒ์ ์์ฒญ์ ๋ณด๋ธ๋ค.
- ์์ฒญ์ด ์ฑ๊ณตํ๋ฉด .then()์ด ์คํ๋๋ค.
- ์๋ฒ์์ ๋ฐ์ ๋ฐ์ดํฐ๋ response.data์ ์ ์ฅ๋๋ค.
- ์์ฒญ์ด ์คํจํ๋ฉด .catch()๊ฐ ์คํ๋๋ค.
โผ๏ธ ์ฃผ์ HTTP ๋ฉ์๋
| ๋ฉ์๋ | ์ญํ |
| axios.get() | ๋ฐ์ดํฐ ์กฐํ |
| axios.post() | ๋ฐ์ดํฐ ์์ฑ |
| axios.put() | ๋ฐ์ดํฐ ์ ์ฒด ์์ |
| axios.patch() | ๋ฐ์ดํฐ ์ผ๋ถ ์์ |
| axios.delete() | ๋ฐ์ดํฐ ์ญ์ |
| .then() | ์์ฒญ ์ฑ๊ณต ์ฒ๋ฆฌ |
| .catch() | ์์ฒญ ์คํจ ์ฒ๋ฆฌ |
โผ๏ธ ์๋ต ๊ฐ์ฒด(Response)
Axios๋ ์์ฒญ์ด ์ฑ๊ณตํ๋ฉด ์๋ต ๊ฐ์ฒด๋ฅผ ๋ฐํํ๋ค.
axios.get("/users")
.then((response) => {
console.log(response.data);
console.log(response.status);
console.log(response.headers);
});
- response.data: ์๋ฒ์์ ๋ฐ์ ๋ฐ์ดํฐ.
- response.status: HTTP ์ํ ์ฝ๋.
- response.headers: ์๋ต ํค๋.
- response.config: ์์ฒญ ์ค์ ์ ๋ณด.
โผ๏ธ React์์ Axios ์ฌ์ฉ
React์์๋ useEffect์ ํจ๊ป ์ฌ์ฉํ๋ ๊ฒฝ์ฐ๊ฐ ๊ฐ์ฅ ๋ง๋ค.
import { useEffect } from "react";
import axios from "axios";
useEffect(() => {
axios.get("/products")
.then((response) => {
console.log(response.data);
})
.catch((error) => {
console.error(error);
});
}, []);
- ์ปดํฌ๋ํธ๊ฐ ์ฒ์ ๋ ๋๋ง๋ ๋ ์๋ฒ์์ ๋ฐ์ดํฐ๋ฅผ ๊ฐ์ ธ์ค๋ ํจํด์ผ๋ก ์์ฃผ ์ฌ์ฉ๋๋ค.
๐ POINT ๐
โ๏ธ Axios๋ Promise ๊ธฐ๋ฐ์ด๋ฏ๋ก .then(), .catch() ๋๋ async/await์ ํจ๊ป ์ฌ์ฉํ ์ ์๋ค. โ๏ธ
โ๏ธ ์๋ฒ์์ ๋ฐ์ ์ค์ ๋ฐ์ดํฐ๋ ๋๋ถ๋ถ response.data๋ฅผ ํตํด ์ฌ์ฉํ๋ค. โ๏ธ
โ๏ธ React์์๋ useEffect ์์์ API๋ฅผ ํธ์ถํ๋ ๊ฒฝ์ฐ๊ฐ ์ผ๋ฐ์ ์ด๋ค. โ๏ธ
โ๏ธ ์ต๊ทผ์๋ JavaScript์ ๋ด์ฅ API์ธ fetch()๋ ๋ง์ด ์ฌ์ฉ๋์ง๋ง,
Axios๋ ์ฝ๋๊ฐ ๊ฐ๊ฒฐํ๊ณ ๋ค์ํ ํธ์ ๊ธฐ๋ฅ์ ์ ๊ณตํด ์ฌ์ ํ ๋ง์ด ์ฌ์ฉ๋๋ค. โ๏ธ
๐ axios ์ค์น ๐
terminal
npm install axios
'<React>' ์นดํ ๊ณ ๋ฆฌ์ ๋ค๋ฅธ ๊ธ
| Asynchronous Operation (๋น๋๊ธฐ ์์ ) (0) | 2026.07.23 |
|---|---|
| Callback Function (์ฝ๋ฐฑ ํจ์) (0) | 2026.07.23 |
| useEffect (์ ์ฆ์ดํํธ) (0) | 2026.07.23 |
| React.js Life Cycle (๋ฆฌ์กํธ์ ์๋ช ์ฃผ๊ธฐ) (0) | 2026.07.23 |
| reducer / Redux / Redux Toolkit (0) | 2026.07.20 |
