Last active
February 12, 2023 05:31
-
-
Save Tim-W-James/a2cfbeefe82f22381919db2f7e928ddc to your computer and use it in GitHub Desktop.
React Lazy Load Named Export #react
This file contains hidden or 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
/** | |
* 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