Skip to content

Instantly share code, notes, and snippets.

@goran-peoski-work
Last active April 13, 2024 19:44
Show Gist options
  • Save goran-peoski-work/d5eb009af95c0d4233b73d6efe5dfb64 to your computer and use it in GitHub Desktop.
Save goran-peoski-work/d5eb009af95c0d4233b73d6efe5dfb64 to your computer and use it in GitHub Desktop.
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