Notice
Recent Posts
Recent Comments
Link
์ผ | ์ | ํ | ์ | ๋ชฉ | ๊ธ | ํ |
---|---|---|---|---|---|---|
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 |
Tags
- css in js
- frontend
- ์ฝ๋์คํ ์ด์ธ
- Prototype
- html
- Router
- ํ๊ณ
- ์ฐธ์กฐ์๋ฃํ
- ์๋ฐ์คํฌ๋ฆฝํธ
- cta button
- ํธ์คํธ์ธ์
- css
- WAI-ARIA
- JS
- Javascript #์ฝ๋์คํ ์ด์ธ
- ๊ณ์ฐ๊ธฐ
- condestates
- self reliance
- cta๋ฒํผ
- ๊ฐ์ฒด์งํฅ
- codestates
- ํ๋กํ ํ์
- ์์์๋ฃํ
- codestate
- JavaScript
- ํ๋ก ํธ์๋
- ๊ฐ๋ฐ์
- ์ฝ๋์คํ ์ด์ค
- CDD
- OOP
Archives
- Today
- Total
jh.nrtv
Fetch ์ฌ์ฉ๋ฒ(get, post, put, delete) , ์ปค์คํ ํ useFetch ๋ณธ๋ฌธ
Network, server
Fetch ์ฌ์ฉ๋ฒ(get, post, put, delete) , ์ปค์คํ ํ useFetch
wlgus3 2023. 1. 26. 16:39โ Fetch ์ฌ์ฉ๋ฒ
๐ธ GET (๋ฐ์ดํฐ ๊ฐ์ ธ์ค๊ธฐ / ์กด์ฌํ๋ ์์์ ์์ฒญ)
fetch('url')
.then((response)=>(response.json()))
.then((data)=>console.log(data))
response๋ ๊ฐ์ฒดํํ
๐ธ POST (๋ฐ์ดํฐ ์์ฑํ๊ธฐ / ์๋ก์ด ์์ ์์ฑ ์์ฒญ)
fetch("http://localhost:3001/blogs", {
method: "POST",
headers: {
"Content-Type": "application/json",
},
body: JSON.stringify({
id: blogs.length + 1,
title: title,
body: body,
author: author,
likes: 0,
}),
})
.then((response) => response.json())
.then((data) => console.log(data));
๐ธ PUT ( ์กด์ฌํ๋ ์์ ๋ณ๊ฒฝ ์์ฒญ )
fetch(`http://localhost:3001/blogs/${id}`, {
method: "PUT",
headers: {
"Content-Type": "application/json",
},
body: JSON.stringify({
...data,
likes: data.likes - 1,
}),
})
.then((response) => response.json())
.then((data) => console.log(data));
console.log("Reject like!");
๐ธ DELETE (๋ฐ์ดํฐ ์ญ์ ํ๊ธฐ / ์กด์ฌํ๋ ์์ ์ญ์ ์์ฒญ)
๊ธฐ๋ณธํํ
fetch(`http://localhost:3001/blogs/${id}`, {
method: "DELETE", //! delete๋ body ํ์์์ด ๋ฉ์๋๋ง ์ ๋ฌํด์ฃผ๋ฉด ๋จ
})
.then((response) => response.json())
.then((data) => console.log(data));
.catch(error=>console.log(error));
โ ์ปค์คํ ํ - useFetch(url)
import { useState, useEffect } from "react";
const useFetch = (url) => {
// const [data,setData] = useState(null)
const [data, setData] = useState(null);
const [isPending, setIsPending] = useState(true);
const [error, setError] = useState(null);
useEffect(() => {
setTimeout(() => {
fetch(url)
.then((res) => {
if (!res.ok) {
throw Error("could not fetch the data for that resource");
}
return res.json();
})
.then((data) => {
setIsPending(false);
setData(data);
setError(null);
})
.catch((err) => {
setIsPending(false);
setError(err.message);
});
}, 1000);
}, [url]);
return [data, isPending, error]; /* return ๋ฌธ์ ์์ฑํด์ฃผ์ธ์. */
};
export default useFetch;
'Network, server' ์นดํ ๊ณ ๋ฆฌ์ ๋ค๋ฅธ ๊ธ
[Deploy] AWS ์ค์ต (0) | 2023.02.02 |
---|---|
[Deploy] AWS (2) | 2023.02.02 |