Skip to content

Instantly share code, notes, and snippets.

@coreyward
Created February 20, 2023 21:45
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save coreyward/b3bb133f501510ab14b119c0864b7ace to your computer and use it in GitHub Desktop.
Save coreyward/b3bb133f501510ab14b119c0864b7ace to your computer and use it in GitHub Desktop.
Example of using React 18 Lazy to trigger code splitting when using an import map to render a Sanity-based list of components
import React, { Suspense } from "react";
const componentMap = {
sectionOne: React.lazy(() => import("./SectionOne")),
sectionTwo: React.lazy(() => import("./SectionTwo")),
};
const Section = ({ _type, ...sectionProps }) => {
const SectionComponent = componentMap[_type];
return (
<Suspense fallback={<div>Loading section…</div>}>
<SectionComponent {...sectionProps} />
</Suspense>
);
};
export default Section;
// Then, to render content:
const Content = ({ sections }) => (
<div>
{sections.map((section) => (
<Section key={section._key} {...section} />
))}
</div>
);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment