I am trying to run an API call with a useEffect()
function in Vite, but it doesn’t seem to run (debugged with console.log()
).
I tried running:
useEffect(() => {
const fetchPosts = async () => {
setLoading(true);
const res = await axios.get('https://jsonplaceholder.typicode.com/posts');
setPosts(res.data);
setLoading(false);
}
fetchPosts();
}, []);
expecting setPosts()
to return the information from the API, but it returns empty. Putting a console.log()
before declaring fetchPosts
does nothing, and shows that useEffect()
doesn’t run.
1