Skip to content

Instantly share code, notes, and snippets.

@tracker1
Created May 6, 2020 15:06
Show Gist options
  • Star 2 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save tracker1/f0b91b0bc07fe67d08ef2d6119707a2d to your computer and use it in GitHub Desktop.
Save tracker1/f0b91b0bc07fe67d08ef2d6119707a2d to your computer and use it in GitHub Desktop.
Code Splitting via lazy import with React
/*
/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>
);
}
/*
/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