Created
June 19, 2018 22:07
-
-
Save shalkam/d2404971429a3c991f52d16d42aed2bb to your computer and use it in GitHub Desktop.
React loadable helper function
This file contains 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 loadable from './loadable'; | |
const AsyncComponent = loadable(import('./my-component')); |
This file contains 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 Loadable from 'react-loadable'; | |
import React from 'react'; | |
import Loading from './loading'; | |
const loadable = (loader, render) => { | |
if (!render) { | |
// eslint-disable-next-line | |
render = (Loaded, props) => { | |
return <Loaded.default {...props} />; | |
}; | |
} | |
return Loadable({ | |
loader: () => loader, | |
render, | |
loading: Loading, | |
delay: 300, | |
timeout: 10000 | |
}); | |
}; | |
export default loadable; |
This file contains 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 PropTypes from 'prop-types'; | |
const Loading = props => { | |
if (props.error) { | |
return <div>Error!</div>; | |
} else if (props.timedOut) { | |
return <div>Taking a long time</div>; | |
} else if (props.pastDelay) { | |
return <div>Loading ...</div>; | |
} | |
return null; | |
}; | |
Loading.propTypes = { | |
error: PropTypes.object, | |
timedOut: PropTypes.bool, | |
pastDelay: PropTypes.bool | |
}; | |
Loading.defaultProps = { | |
error: null, | |
timedOut: false, | |
pastDelay: false | |
}; | |
export default Loading; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment