โ ํน์ง
โ ๏ธ fetch์ axios๋ฅผ ์ค axios๋ฅผ ๋ง์ด ์ฌ์ฉํ์ฌ axios ํ
์คํธ
fetch๋ ์์๋ณด๊ธฐ ์ถ์ฒ๋๋ฆฝ๋๋ค!
npm install axios
โ package.json ํ์ผ ํ์ธ
import axios from 'axios';
// 1.
axios.get("URL")
.then((response) => { // ์ฑ๊ณต
console.log(response)
})
.catch((error) => { // ์คํจ
console.log(error)
})
// 2.
async function axiosGet(){
try {
const response = await axios.get("URL");
console.log(response)
} catch(error){
console.log(error)
}
}
โ
์์ฒญ ๋ฉ์๋ ๋ช
๋ น์ด
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]])
โ ๋ํ์ ์ผ๋ก ์๋ 4๊ฐ์ง๋ฅผ ์ฌ์ฉ
// ๊ฐ์ ธ์ค๊ธฐ ์ฝ๊ธฐ
GET : axios.get(url[, config])
// ๋ฐ์ดํฐ ์ถ๊ฐ
POST : axios.post(url, data[, config])
// ๋ฐ์ดํฐ ์์
PUT : axios.put(url, data[, config])
// ๋ฐ์ดํฐ ์ญ์
DELETE : axios.delete(url[, config])
๐ Axios ์ฐธ๊ณ
https://axios-http.com/
https://axios-http.com/kr/docs/intro
Inpa Dev
โ ./data/data.json ์์ฑ (src์ ๋์ผํ ์์น, ํ์ ์์น)
{
"test": [
{
"id":1,
"name":"์ด๋ฆ1",
"desc":"ํ
์คํธ ์ค"
},
{
"id":2,
"name":"์ด๋ฆ2",
"desc":"ํ
์คํธ ์ค"
},
{
"id":3,
"name":"์ด๋ฆ3",
"desc":"ํ
์คํธ ์ค"
},
{
"id":4,
"name":"์ด๋ฆ4",
"desc":"ํ
์คํธ ์ค"
},
{
"id":5,
"name":"์ด๋ฆ5",
"desc":"ํ
์คํธ ์ค"
}
]
}
โ Axios๋ฅผ ์ฐ์ตํ๊ธฐ ์ํด ๊ฐ์ง API ์๋ฒ๋ฅผ ๋ง๋ค๊ณ ์คํ!!
port : React 3000 ์ฌ์ฉ / json-server 4000 ์คํ.
$ npx json-server ./data/data.json --port 4000
๊ธ๋ก๋ฒ ์ค์น ํ json-server ./data.json --port 4000 ์ฌ์ฉ ๊ฐ๋ฅ
npm install -g json-server
โ
์คํ ์๋ฃ ํ๋ฉด
Resources ๊ฒฝ๋ก๋ฅผ ํ์ธํ๋ฉด ์ ์์ ์ผ๋ก ์ถ๋ ฅ์ ํ์ธ ํ ์ ์์ต๋๋ค. ๐
React ์คํํ๊ณ json-server ๋ฐ๋ก ์คํํ๋ค๋ณด๋ ๋ถํธํ๋
ํ๋ฒ์ react์ json-server๋ฅผ ์คํ ์ํค๋๋ก ํด๋ณด๊ฒ ์ต๋๋ค!
๐๐
โ concurrently ๋ผ์ด๋ธ๋ฌ๋ฆฌ๋ฅผ ์ด์ฉํ์ฌ React์ json-server ํ๋ฒ์ ์คํํ๊ธฐ.
npm install concurrently --save
๐ ์ฐธ๊ณ
https://www.npmjs.com/package/concurrently
โ๏ธ package.json์์ concurrently ์ค์น ํ์ธ.
โ package.json์์ scripts dev๋ฅผ ์ถ๊ฐํด์ ํ ์คํธ
"dev": "concurrently \"npm run start\" \"json-server ./data/data.json --port 4000\""
โ๏ธ npm run dev ๋ฅผ ์คํ
localhost:3000
localhost:4000/test
ํ์ธ ์๋ฃ๐
๐ axios ํ ์คํธ ์งํ!!
๐ GET ํ ์คํธ
import axios from "axios"
function AxiosTest(){
axios.get("http://localhost:4000/test")
.then((res) => {
console.log(res)
})
.catch((err) => {
console.log(err)
})
return (
<>
</>
)
}
export default AxiosTest;
โ๏ธ ํ์ธ
โ ERROR
// โ ๏ธ ์๋ชป๋ ์ฃผ์ ์
๋ ฅ.
axios.get("http://localhost:4000/velog")
.then((res) => {
console.log(res)
})
.catch((err) => {
console.log(err)
})
๐ POST ํ ์คํธ
import axios from "axios"
function AxiosTest(){
const addData = {
name:"์ด๋ฆ6",
desc:"ํ
์คํธ ์ค6"
}
axios.post("http://localhost:4000/test",addData)
.then((res) => {
console.log(res)
})
.catch((err) => {
console.log(err)
})
return (
<>
</>
)
}
export default AxiosTest;
โ ๏ธ ๋ ๋๋ง ์ axios.post๊ฐ ์คํ ๋์ด์ ์ฌ๋ฌ๊ฐ๊ฐ ์ถ๊ฐ๊ฐ ๋ ๊ฒ์ ํ์ธํ ์ ์์ต๋๋ค.
useEffect ๋๋ ์ด๋ ํ ๋ฐ์์ ์ํด ํ์ํ ๊ฒฝ์ฐ์ ์คํ ํ๋๋ก ์์ ํด์ฃผ์ธ์!
๐ PUT ํ ์คํธ
import axios from "axios"
function AxiosTest(){
const putChange = () => { // ํด๋ฆญ ์ ๋ณ๊ฒฝํ๋๋ก
axios.put("http://localhost:4000/test/4",{
name:"velog",
desc:"์์ด๋4๋ก ๋ณ๊ฒฝ"
})
.then((res) => {
console.log(res);
})
.catch((err) => {
console.log(err);
})
}
return (
<>
<button
type="button"
onClick={() => putChange()}>
ID 4 ๋ณ๊ฒฝํ๊ธฐ
</button>
</>
)
}
export default AxiosTest;
โ๏ธ ๋ฒํผ ํด๋ฆญ ์ - ์์ ์๋ฃ!
โ๏ธ json ํ์ผ ํ์ธํ๋ฉด ๋ณ๊ฒฝ์ด ๋์ด ์๋ค.
๐ DELETE
import axios from "axios"
function AxiosTest(){
const axiosDelete = () => { // id 5 ์ญ์
axios.delete("http://localhost:4000/test/5")
.then((res) => {
console.log(res);
})
.catch((err) => {
console.log(err);
})
}
return (
<>
<button
type="button"
onClick={() => axiosDelete()}>
ID 5 ์ญ์ ํ๊ธฐ
</button>
</>
)
}
export default AxiosTest;
โ๏ธ ๋ฒํผ ํด๋ฆญ ์ - ์ญ์ ์๋ฃ!
โ๏ธ json ํ์ผ ํ์ธ
๐ํ ์คํธ์ฉ
import axios from "axios"
function AxiosTest(){
const axiosGet = () => {
axios.get("http://localhost:4000/test")
.then((res) => {
console.log(res);
})
.catch((err) => {
console.log(err);
})
}
const axiosPost = () => {
axios.post("http://localhost:4000/test",{
name:"์ถ๊ฐ!",
desc:"Post"
})
.then((res) => {
console.log(res);
})
.catch((err) => {
console.log(err);
})
}
const axiosPut = () => { // โ ๏ธ id๊ฐ ์๋ ๊ฒฝ์ฐ error
axios.put("http://localhost:4000/test/4")
.then((res) => {
console.log(res);
})
.catch((err) => {
console.log(err);
})
}
const axiosDelete = () => { // โ ๏ธ id๊ฐ ์๋ ๊ฒฝ์ฐ error
axios.delete("http://localhost:4000/test/6")
.then((res) => {
console.log(res);
})
.catch((err) => {
console.log(err);
})
}
return (
<>
<button type="button" onClick={() => axiosGet()}>ํ์ธ</button>
<hr />
<button type="button" onClick={() => axiosPost()}>์ถ๊ฐ</button>
<hr />
<button type="button" onClick={() => axiosPut()}>ID 4 ๋ณ๊ฒฝํ๊ธฐ</button>
<hr />
<button type="button" onClick={() => axiosDelete()}>ID 5 ์ญ์ ํ๊ธฐ</button>
</>
)
}
export default AxiosTest;
โ ๊ฐ๋จํ๊ฒ get, post, put, delete ํ์ธ ์๋ฃ ๐
import axios from "axios"
import { useEffect, useState } from "react";
import styled from "styled-components";
function AxiosTest(){
const [listData, setlistData] = useState([]); // default []
const URL = "http://localhost:4000/test";
// ์ด๊ธฐ ๋ถ๋ฌ์ค๊ธฐ
const loadAxios = async() => {
try {
const res = await axios.get(URL)
setlistData(res.data); // state data ๊ฐ ์
๋ ฅ
}catch(error) {
console.log(error)
}
}
// ์ถ๊ฐ
const postAxios = async(pushData) => { // ์์ ํ๋ item, ์์ ๊ฐ
try{
await axios.post(`${URL}`, {...pushData})
}catch(error) {
console.log(error)
}
}
// checked ์์
const putAxios = async(putItem, putData) => { // ์์ ํ๋ item, ์์ ๊ฐ
try{
await axios.put(`${URL}/${putItem.id}`, { ...putItem, ...putData})
}catch(error) {
console.log(error)
}
}
// ์ญ์
const deleteAxios = async(putItem) => { // ์ญ์ ๋ฐ์ดํฐ
try{
await axios.delete(`${URL}/${putItem.id}`)
}catch(error) {
console.log(error)
}
}
useEffect(()=>{
loadAxios();
},[])
const addItem = () => { // ์ถ๊ฐ ๋ฒํผ
const testData = {
id: `test_${Math.random() * 1000}`,
name:"ํ
์คํธ Name",
checked:false
};
// setlistData(prev => [...prev, testData])
setlistData(prev => prev.concat(testData))
postAxios(testData);
}
const toggleChecked = (liItem) => { // ์์ ๋ฒํผ
const toggleCheck = {
checked: !liItem.checked
}
setlistData(
listData.map(item =>
item.id === liItem.id ? {...item, ...toggleCheck}: item
)
)
putAxios(liItem, toggleCheck); // axios put ์์ฒญ
}
const removeItem = (liItem) =>{ // ์ญ์ ๋ฒํผ
setlistData(
listData.filter(item =>
item.id !== liItem.id && item
)
)
deleteAxios(liItem);
}
return (
<div>
<AddBtnStyle onClick={() => addItem()}>์ถ๊ฐํ๊ธฐ</AddBtnStyle>
<hr />
<ul>
{
listData.map((item,idx) => {
return <LiStyle key={idx}>
<p>name: {item.name}</p>
<button type="button" onClick={()=>toggleChecked(item)}>
{item.checked ? 'โ
' : 'โฌ' }
</button>
<button type="button" onClick={()=>removeItem(item)}>โ</button>
</LiStyle>
})
}
</ul>
</div>
)
}
export default AxiosTest;
// styled-component
const AddBtnStyle = styled.button.attrs({
type:'button'
})`
margin:10px 0;
padding:5px;
border:1px solid #dbdbdb;
background:transparent;
cursor:pointer;
`;
const LiStyle = styled.li`
margin-top:5px;
border-top:1px solid #dbdbdb;
& > button {
margin:0 2px;
padding:5px;
}
`;
โ ๐ ํ ์คํธ์ฉ
๊ฐ์ฌํฉ๋๋ค. ๐