Skip to content

Instantly share code, notes, and snippets.

@jacobp100
Last active March 23, 2018 22:39
Show Gist options
  • Select an option

  • Save jacobp100/0eaf20a0a89123a7e18c7cac56120a8e to your computer and use it in GitHub Desktop.

Select an option

Save jacobp100/0eaf20a0a89123a7e18c7cac56120a8e to your computer and use it in GitHub Desktop.
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