Skip to content

Instantly share code, notes, and snippets.

@toruticas
Created February 1, 2020 21:04
Show Gist options
  • Select an option

  • Save toruticas/71a5fda06ff6c37856245144ccacccf1 to your computer and use it in GitHub Desktop.

Select an option

Save toruticas/71a5fda06ff6c37856245144ccacccf1 to your computer and use it in GitHub Desktop.
import React from 'react'
class Suspense extends React.Component {
state = {
promise: null
};
componentDidCatch(error) {
// Oh my god, this is the desired Promise 😬
if (error instanceof Promise) {
this.setState({ promise: e }, () => {
// The promise has finished 🤓. Let's render the children
error.then(() => {
this.setState({ promise: null });
});
});
}
// Nothing to do here 😒. Propagate the Error to the next stack
else {
throw e;
}
}
render() {
if (this.state.promise) {
return this.props.fallback;
}
return this.props.children;
}
}
export { Suspense }
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment