Last active
April 13, 2024 19:44
-
-
Save goran-peoski-work/d5eb009af95c0d4233b73d6efe5dfb64 to your computer and use it in GitHub Desktop.
Dynamic import of omponent
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
import React, { ComponentType, LazyExoticComponent } from "react"; | |
type Transformer = <T>(name: keyof T) => (module: T) => { default: T[keyof T] }; | |
const transformer: Transformer = ( | |
(name) => (module) => ({ default: module[name] }) | |
); | |
// eslint-disable-next-line @typescript-eslint/no-explicit-any | |
type LazyImport = <T extends ComponentType<any>>(path: string, name: string) => LazyExoticComponent<T>; | |
export const lazyImport: LazyImport = ( | |
(path, name) => React.lazy( | |
() => import(path).then(transformer(name)) | |
) | |
); | |
const Admin = lazyImport("pages/Admin/Admin", "Admin") |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment