-
-
Save StevenLangbroek/cd6c68681fb78f1db0d8 to your computer and use it in GitHub Desktop.
React Loadable Component
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'; | |
import Spinner from 'react-spinner'; | |
export const loadable = (hasLoadedTest, Component) => props => hasLoadedTest(props) ? <Component { ...props } /> : <Spinner />; |
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
// Will only render if `items` is present, otherwise renders a spinner | |
const LoadableList = loadable( | |
({ items }) => !!items, | |
List // List is a React component | |
); | |
/* | |
render() { | |
return <LoadableList items={ this.state.items } /> | |
} | |
*/ | |
// Will only render if `image` is present, otherwise renders a spinner | |
const LoadableImage = loadable( | |
({ image }) => !!image, | |
({ image: { URL, width, height }, description }) => | |
<img src={ URL } width={ width } height={ height } alt={ description } /> | |
); | |
/* | |
render() { | |
return <LoadableImage image={ this.state.image } /> | |
} | |
*/ |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Nice one man!