You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Dan Abrimov has a great react example that covers best practices for a promise in react.
this is a writeup of the tweet describing proper promise chaining for a ui update
*/
// Bad Practice to use catch when updating a ui in React.
Promise.resolve()
.then(() => {
this.setState({ loaded: true})
})
.catch(() => {
console.log('Swallowed')
})
// Your catch() is going to catch any error thrown in the then() chain before it, including the one caused by a render() due to a setState() call.
// If you do not want to catch errors resulting from setState(), and want to only catch network failures (let's imagine your Promise.resolve() is actually a fetch() ), you want to use the second then() argument instead:
// Correct practice for catching network failures and not for updating ui in react.
componentDidMount() {
Promise.resolve()
.then(
() => { this.setState({ loaded: true}) },
(err) => { console.log("An error occoured (but not in setState!)", err) }
);
}
// In this case, unless you catch() later in the chain, the error in render() will be uncaught and, with a good Promise polyfill (or with native Promises in Chrome or maybe other browsers), displayed