Skip to content

Conversation

@hshamsadd
Copy link

@bnhoogendijk bnhoogendijk self-assigned this Oct 10, 2025
Copy link

@bnhoogendijk bnhoogendijk left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nice coding this week again!!!

Comment on lines +3 to +17
export default function useFetch(url) {
const [data, setData] = useState(null);
const [loading, setLoading] = useState(true);

useEffect(() => {
setLoading(true);
fetch(url)
.then((res) => res.json())
.then((data) => setData(data))
.finally(() => setLoading(false));
//url is the dependency here because if the url changes we want to refetch the data
}, [url]);

return { data, loading };
}

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is great ✨! You could consider moving the url parameter to a separate function inside the hook and setup the hook with some generic info like the baseUrl.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants