Last active
November 30, 2020 21:57
-
-
Save kellenmace/d7ea622441af3fbc80f8f7b094b9bae8 to your computer and use it in GitHub Desktop.
Use React.lazy() to lazily load NPM packages on the client
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, { 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