Last active
May 21, 2020 11:59
-
-
Save srdjanRakic/8cc94ec71a488d4812a5c89824a0d66e to your computer and use it in GitHub Desktop.
Bundle your components into chunks using React.lazy. Let's say you have a folder with several components, A, B, and C, D which should be bundled together.
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
// Assuming A through D are all regular React component declarations... | |
// components/A.js | |
export default function A() { | |
return <div>Hello, World!</div> | |
} | |
// Use React.lazy to create a dynamically loaded version of the component | |
// components/index.js | |
export const A = React.lazy(() => import(/* webpackChunkName: "top" */ "./A")); | |
export const B = React.lazy(() => import(/* webpackChunkName: "top" */ "./B")); | |
export const C = React.lazy(() => import(/* webpackChunkName: "bottom" */ "./C")); | |
export const D = React.lazy(() => import(/* webpackChunkName: "bottom" */ "./D")); | |
// The first time React renders a dynamically loaded component, it may | |
// render "Loading..." as a placeholder until the component is ready | |
<React.Suspense fallback="Loading..."> | |
<App /> | |
</React.Suspense> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment