Take the following code in my React component:
const getPhotos = async (page?: number) => {
setLoading(true);
try {
const data = await endpoints.getCuratedPhotos(page)
if (!data) {
setError('No Photos found')
setLoading(false);
} else {
setPhotos(data);
setSearchQuery(null)
setLoading(false);
}
} catch (e) {
console.log('error something went wrong', e);
setError(`error something went wrong, ${e}`)
setLoading(false);
}
}
useEffect(() => {
getPhotos();
}, [])
Is it necessary or even good practice to have an if statement checking for !data
in the try block? Is there scenario where this would be triggered other than a developer changing the URL in the endpoint? Is it sufficient to just have my catch block catch all the errors? Thank you in advance.