Loading...
Great question! I've run into this before. The key thing to keep in mind is to follow the official documentation closely and test your implementation with edge cases. Happy to elaborate if you need more specifics.
asked 2 months ago
1
6.3K
I wrote a custom hook for fetching data. Is this the right way to do it, or am I missing something?
function useFetch(url: string) {
const [data, setData] = useState(null);
const [loading, setLoading] = useState(true);
const [error, setError] = useState(null);
useEffect(() => {
fetch(url)
.then(res => res.json())
.then(setData)
.catch(setError)
.finally(() => setLoading(false));
}, [url]);
return { data, loading, error };
}
Should I use AbortController? What about race conditions?
Great question! I've run into this before. The key thing to keep in mind is to follow the official documentation closely and test your implementation with edge cases. Happy to elaborate if you need more specifics.
1
6
2