Fazza Razaq Amiarso• 2,360
@fazzaamiarso
Posted
Hello Clytax! Nice solution!
The easiest way to handle the error is by throwing an exception on the response and appending .catch
on your promise chain. https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Promise/catch
.then(res => {
if(!res.ok) throw Error('Failed to fetch'); //exception;
return res.json();
}).then().then().catch(error => console.log(error.message) )
I also recommend you to read this article to answer your state question https://kentcdodds.com/blog/application-state-management-with-react
I hope it helps! Cheers!
0