Input + Output = True
๋๊ธฐ ๋น๋๊ธฐ / ์๋ฒ ํต์ (feat. react axios) ๋ณธ๋ฌธ
๐ ๋๊ธฐ ๋น๋๊ธฐ / ์๋ฒ ํต์ (feat. react axios)
โ ๋๊ธฐ์ ๋น๋๊ธฐ
1. ๋๊ธฐ (synchronous: ๋์์ ์ผ์ด๋๋)
์์ฒญ๊ณผ ๊ฒฐ๊ณผ๊ฐ ๋์์ ์ผ์ด๋๋ค๋ ์๋ฏธ.
์์ฒญ์ ํ๋ฉด ์๊ฐ์ด ๊ฑธ๋ฆฌ๋๋ผ๋ ์์ฒญํ ์๋ฆฌ์์ ๊ฒฐ๊ณผ๊ฐ ์ถ๋ ฅ๋์ด์ผ ํ๋ค.
์ฅ์ : ์ค๊ณ๊ฐ ๊ฐ๋จํ๊ณ ์ง๊ด์ ์ด๋ค.
๋จ์ : ์ด๋ฒ ๊ฒฐ๊ณผ๊ฐ ์ฃผ์ด์ง ๋๊น์ง ๋ค์ ์์
์งํ์ ๋ชปํ๊ณ ๋๊ธฐํด์ผ ํ๋ค.
2. ๋น๋๊ธฐ (Asynchronous: ๋์์ ์ผ์ด๋์ง ์๋)
์์ฒญ๊ณผ ๊ฒฐ๊ณผ๊ฐ ๋์์ ์ผ์ด๋์ง ์๋๋ค๋ ์๋ฏธ.
์์ฒญํ ๊ทธ ์๋ฆฌ์์ ๊ฒฐ๊ณผ๊ฐ ์ฃผ์ด์ง์ง ์์.
๋ฐ์ดํฐ๋ฅผ ๋ถ๋ฌ์ค๋ ์๋ฒ์์ ํต์ ์ ์ฐจ์์ ์ฃผ๋ก ์ฌ์ฉ๋๋ค.
์ฅ์ : ์์ฒญ์ ๋ฐ๋ฅธ ๊ฒฐ๊ณผ๊ฐ ๋ฐํ๋๋ ์๊ฐ ๋์ ๋ค๋ฅธ ์์
์ ์งํํ ์ ์๋ค.
๋จ์ : ๋๊ธฐ์๋ณด๋ค ์ค๊ณ๊ฐ ๋ค์ ๋ณต์กํ๋ค.
์๋ฅผ ๋ค์๋ฉด
๋๊ธฐ: A ์์ ์์ฒญ > A ์์ ๊ฒฐ๊ณผ ์ถ๋ ฅ > B ์์ ์์ฒญ > B ์์ ๊ฒฐ๊ณผ ์ถ๋ ฅ > C ์์ ์์ฒญ > C ์์ ๊ฒฐ๊ณผ ์ถ๋ ฅ
๋น๋๊ธฐ: A ์์ ์์ฒญ > B ์์ ์์ฒญ > C ์์ ์์ฒญ > A + B + C ์์ ๊ฒฐ๊ณผ ์ถ๋ ฅ
์ด๋ ๊ฒ ์ดํดํ ์ ์๋ค.
โ axios๋?
์๋ฐ์คํฌ๋ฆฝํธ์์ API๋ฅผ ์ฐ๋ํ๊ธฐ ์ํด์(์๋ฒ์ ํต์ ) ๋ณดํต Fetch API๋ฅผ ์ฌ์ฉํ๋ค.
๋ฆฌ์กํธ๋ ์๋ฐ์คํฌ๋ฆฝํธ built-in ๋ผ์ด๋ธ๋ฌ๋ฆฌ์ค ํ๋์ธ Fetch API๋ผ๋ API ์ฐ๋ ๋ชจ๋์ ์ฌ์ฉํ๋ค.
ํ์ง๋ง ๋ฆฌ์กํธ์์๋ axios ์ฌ์ฉ์ ์ ํธํ๋ค.
axios ํน์ง
-Fetch API ๋ณด๋ค ๊ฐ์ํ๋ ๋ฌธ๋ฒ
-Axios๋ ์ด์ํ๊ฒฝ์ ๋ฐ๋ผ XMLHttpRequest ๊ฐ์ฒด ๋๋ Node.js์ HTTP API๋ฅผ ์ฌ์ฉํ๋ค
-promise API๋ฅผ ์ฌ์ฉํ๋ค(.then ์ฌ์ฉ ๊ฐ๋ฅ)
-HTTP ์์ฒญ๊ณผ ์๋ต์ JSON ํํ๋ก ์๋ ๋ณ๊ฒฝ
-CSRF ๋ณดํธ ๊ธฐ๋ฅ์ด ๋ด์ฅ๋์ด ์๋ค
fetch api post
fetch("https://localhost:3000/user/post", {
method: "POST",
headers: {
"Content-Type": "application/json",
},
body: JSON.stringify({
id: "userA",
job: "developer",
}),
}).then((response) => console.log(response));
axios post
axios({
method: 'post',
url: 'https://localhost:3000/user',
data: {
userName: 'user',
job: 'developer'
}
}).then((response) => console.log(response));
โ axios ์ฌ์ฉ๋ฒ
axios ์ค์น
yarn add axios
npm i axios
//ํ๋ก์ ํธ ํ์ผ ์๋จ import
import axios from 'axios'
axios ๋ฌธ๋ฒ
// POST ์์ฒญ ์ ์ก
axios({
method: 'post',
url: '/user/12345',
data: {
firstName: 'Hello',
lastName: 'World'
}
});
axios({
method: 'get',
url: 'http://server/api',
responseType: 'stream'
})
.then(function (response) {
console.log(response)
});
// GET ์์ฒญ ์ ์ก (๊ธฐ๋ณธ๊ฐ)
axios('/user/12345');
axios.request(config)
axios.get(url[, config])
axios.delete(url[, config])
axios.head(url[, config])
axios.options(url[, config])
axios.post(url[, data[, config]])
axios.put(url[, data[, config]])
axios.patch(url[, data[, config]])
์ฐธ๊ณ
https://axios-http.com/kr/docs/intro
์์ํ๊ธฐ | Axios Docs
์์ํ๊ธฐ ๋ธ๋ผ์ฐ์ ์ node.js์์ ์ฌ์ฉํ ์ ์๋ Promise ๊ธฐ๋ฐ HTTP ํด๋ผ์ด์ธํธ ๋ผ์ด๋ธ๋ฌ๋ฆฌ Axios๋? Axios๋ node.js์ ๋ธ๋ผ์ฐ์ ๋ฅผ ์ํ Promise ๊ธฐ๋ฐ HTTP ํด๋ผ์ด์ธํธ ์ ๋๋ค. ๊ทธ๊ฒ์ ๋ํ ์ ๋๋ค(๋์ผํ ์ฝ
axios-http.com
'๐จFrontEnd > React' ์นดํ ๊ณ ๋ฆฌ์ ๋ค๋ฅธ ๊ธ
๋ฆฌ์กํธ axios instance & interceptor/ axios ์๋ฌ ํธ๋ค๋ง (0) | 2023.02.05 |
---|---|
๋ฆฌ์กํธ ์นด์นด์ค ๋ก๊ทธ์ธ (ํ๋ก ํธ ์์ ๋ก๊ทธ์ธ) (0) | 2023.02.01 |
๋ฆฌ์กํธ ๋ผ์ดํ์ฌ์ดํด(ํด๋์คํ vs ํจ์ํ), React hooks (0) | 2023.01.08 |
redux persist ์ฌ์ฉ๋ฒ (๋ฆฌ๋์ค ์๋ก๊ณ ์นจ ๋ฐ์ดํฐ ์ ์ง) (0) | 2023.01.05 |
React - redux ์ดํดํ๊ธฐ (๋ฆฌ๋์ค ๊ฐ๋ ์ ๋ฆฌ) (1) | 2023.01.04 |