-
-
Save jrson83/7d6c0f91d1b0d0316c36793baa54a548 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