On this page of the React docs:
https://reactjs.org/docs/faq-ajax.html
A code comment says...
Note: it's important to handle errors here instead of a catch() block so that we don't swallow exceptions from actual bugs in components.
...about handling errors in the second argument to the second .then
after fetch
. The complete snippet from the docs is:
useEffect(() => {
fetch("https://api.example.com/items")
.then(res => res.json())
.then(
(result) => {
setIsLoaded(true);
setItems(result);
},
// Note: it's important to handle errors here
// instead of a catch() block so that we don't swallow
// exceptions from actual bugs in components.
(error) => {
setIsLoaded(true);
setError(error);
}
)
}, [])
It doesn't elaborate on this advice, and I've seen many examples of working React code using catch
to handle errors from API calls. I've tried Googling but can't find any clarification.
Could someone please give me a more detailed explanation as to why I shouldn't use catch
to deal with errors that arise when making a fetch
API call in a useEffect
hook?
Or are there some situations when it's ok to do so, but others when it's not?
What is meant by "swallow exceptions [...] in components" ?
Does this rule/guideline apply to all of React, or just to API calls?
Or just to the useEffect
hook or componentDidMount
lifecycle method?