Created
May 6, 2020 15:06
-
-
Save tracker1/f0b91b0bc07fe67d08ef2d6119707a2d to your computer and use it in GitHub Desktop.
Code Splitting via lazy import with React
This file contains 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
/* | |
/feature/load-component-import.js | |
This is used by any async loading areas of the application | |
import load from '../load-component-import.js'; | |
const FooComponentAsync = load(() => import('./FooComponent'))'; | |
*/ | |
import React, { Suspense } from 'react'; | |
import Loading from './app/Loading'; | |
// wrap a component into a lazy loader - for use with components below | |
export default function loadComponent(importStatement) { | |
const Component = React.lazy(importStatement); | |
return (props) => ( | |
<Suspense fallback={<Loading />}> | |
<Component {...props} /> | |
</Suspense> | |
); | |
} |
This file contains 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
/* | |
/feature/some-section/index.js | |
This is what is referenced in the router for this section. | |
*/ | |
import load from '../load-component-import.js' | |
export default load(() => import('./SectionComponent.jsx')); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment