Skip to content

Instantly share code, notes, and snippets.

@akbarjondev
Created January 7, 2024 13:45
Show Gist options
  • Save akbarjondev/327de14c72f906495210fa264258371f to your computer and use it in GitHub Desktop.
Save akbarjondev/327de14c72f906495210fa264258371f to your computer and use it in GitHub Desktop.
Custom lazy function in React
export const lazy = (componentFactoryPromise) => {
const componentPromise = componentFactoryPromise()
let Component
componentPromise.then(C => {
Component = C.default
})
return (props) => {
if(!Component) {
throw componentPromise
}
return <Component {...props} />
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment