Skip to content

Instantly share code, notes, and snippets.

@Tim-W-James
Last active February 12, 2023 05:31
Show Gist options
  • Save Tim-W-James/a2cfbeefe82f22381919db2f7e928ddc to your computer and use it in GitHub Desktop.
Save Tim-W-James/a2cfbeefe82f22381919db2f7e928ddc to your computer and use it in GitHub Desktop.
React Lazy Load Named Export #react
/**
* Lazy load a module with a named export
*
* @param {string} path - path for the import
* @param {string} namedExport - named exported member of the module
*/
const lazyLoad = (path: string, namedExport: string) =>
lazy(() =>
namedExport
? import(path).then((module) => ({ default: module[namedExport] }))
: import(path)
);
export default lazyLoad;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment