Skip to content

Instantly share code, notes, and snippets.

@kellenmace
Last active November 30, 2020 21:57
Show Gist options
  • Select an option

  • Save kellenmace/d7ea622441af3fbc80f8f7b094b9bae8 to your computer and use it in GitHub Desktop.

Select an option

Save kellenmace/d7ea622441af3fbc80f8f7b094b9bae8 to your computer and use it in GitHub Desktop.
Use React.lazy() to lazily load NPM packages on the client
import React, { Suspense } from "react"
import hasMounted from "../hooks/useHasMounted"
const SomeNpmPackage = React.lazy(() => import("some-npm-package"))
function LazyLoadedComponent() {
if (!hasMounted) return null
return (
<Suspense fallback={<p>Loading...</p>}>
<SomeNpmPackage />
</Suspense>
)
}
export default LazyLoadedComponent
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment