Created
February 1, 2020 21:04
-
-
Save toruticas/71a5fda06ff6c37856245144ccacccf1 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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