Last active
April 19, 2018 10:38
-
-
Save jpgorman/9a6a10cf1f182c4bf0818b1f88273d5a to your computer and use it in GitHub Desktop.
Redux loader example
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 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