doc: add useEffect hooks

This commit is contained in:
Leon Xu committed 2023-01-30 16:17:44 -08:00
1 parent cd5a41354b
commit 1866e76812
1 file changed
+27
+27
View File
@@ -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>