Skip to content

Instantly share code, notes, and snippets.

@srdjanRakic
Last active May 21, 2020 11:59
Show Gist options
  • Save srdjanRakic/8cc94ec71a488d4812a5c89824a0d66e to your computer and use it in GitHub Desktop.
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.
// 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