Last active
March 23, 2018 22:39
-
-
Save jacobp100/0eaf20a0a89123a7e18c7cac56120a8e 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
| class Timeout extends React.Component { | |
| static LOADED = 0 | |
| static LOADING = 1 | |
| static TIMED_OUT = 2 | |
| state = { mode: Timeout.LOADED } | |
| componentDidCatch(maybePromise) { | |
| if (!(maybePromise instanceof Promise)) { | |
| const notAPromise = maybePromise | |
| throw notAPromise | |
| } | |
| this.setState({ mode: Timeout.LOADING }) | |
| setTimeout(() => { | |
| this.setState(state => ( | |
| state.mode === Timeout.LOADING | |
| ? { mode: Timeout.TIMED_OUT } | |
| : null | |
| )) | |
| }, this.props.ms) | |
| maybePromise.then(() => { | |
| this.setState({ mode: Timeout.LOADED }) | |
| }) | |
| } | |
| render() { | |
| switch (this.state.mode) { | |
| case Timeout.LOADED: | |
| return this.props.children(false) | |
| case Timeout.LOADING: | |
| return null | |
| case Timeout.TIMED_OUT: | |
| return this.props.children(true) | |
| } | |
| } | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment