Skip to content

Instantly share code, notes, and snippets.

@goran-peoski-work
Last active Jul 7, 2021
Embed
What would you like to do?
Dynamic import of omponent
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