Skip to content

Instantly share code, notes, and snippets.

@jpgorman
Last active April 19, 2018 10:38
Show Gist options
  • Save jpgorman/9a6a10cf1f182c4bf0818b1f88273d5a to your computer and use it in GitHub Desktop.
Save jpgorman/9a6a10cf1f182c4bf0818b1f88273d5a to your computer and use it in GitHub Desktop.
Redux loader example
class IsLoading extends React.Component {
constructor(props) {
super(props)
this.state = { isLoading: false }
}
async componentDidCatch(err) {
if (err instanceof Promise) {
this.setState({ isLoading: true })
await err
this.setState({ isLoading: false })
} else {
throw err
}
}
render() {
 return this.props.children(this.state.isLoading)
}
}
export const Loader = (props) => (
<IsLoading>
{(loading: boolean) => (loading  ? 'Loading...' : props.children)}
</IsLoading>
)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment