doc: add useEffect hooks
This commit is contained in:
1 parent
cd5a41354b
commit
1866e76812
1 file changed
+27
@@ -15,6 +15,33 @@ const Home = () => {
|
||||
const [allPosts, setAllPosts] = useState(null);
|
||||
const [searchText, setSearchText] = useState("");
|
||||
|
||||
useEffect(() => {
|
||||
const fetchPosts = async () => {
|
||||
setLoading(true);
|
||||
|
||||
try {
|
||||
const response = await fetch(
|
||||
"https://dalle-image-generator-server.onrender.com/api/v1/posts",
|
||||
{
|
||||
method: "GET",
|
||||
headers: {
|
||||
"Content-Type": "application/json",
|
||||
},
|
||||
}
|
||||
);
|
||||
if (response.ok) {
|
||||
const result = await response.json();
|
||||
setAllPosts(result.data.reverse);
|
||||
}
|
||||
} catch (error) {
|
||||
} finally {
|
||||
setLoading(false);
|
||||
}
|
||||
};
|
||||
|
||||
fetchPosts();
|
||||
}, []);
|
||||
|
||||
return (
|
||||
<section className="max-w-7xl mx-auto">
|
||||
<div>
|
||||
|
||||
Reference in new issue
Block a user