From a2ed9cdf3a821887cad18167acd49e6ba06ad4f3 Mon Sep 17 00:00:00 2001 From: ColorFilter Date: Fri, 15 Sep 2023 18:26:44 +0900 Subject: [PATCH] =?UTF-8?q?=F0=9F=90=9B=20fix(react-utils.ts):=20change=20?= =?UTF-8?q?type=20name=20from=20LazyImportType=20to=20NamedComponents=20fo?= =?UTF-8?q?r=20better=20clarity=20and=20semantics=20=E2=9C=A8=20feat(react?= =?UTF-8?q?-utils.ts):=20add=20error=20handling=20to=20throw=20an=20error?= =?UTF-8?q?=20if=20the=20requested=20component=20is=20not=20found=20in=20t?= =?UTF-8?q?he=20module?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/base/utils/react-utils.ts | 9 ++++++--- 1 file changed, 6 insertions(+), 3 deletions(-) diff --git a/src/base/utils/react-utils.ts b/src/base/utils/react-utils.ts index 6d79178..11651ef 100644 --- a/src/base/utils/react-utils.ts +++ b/src/base/utils/react-utils.ts @@ -2,7 +2,7 @@ import { ComponentType, lazy } from 'react'; type ComponentName = string; type Loader = () => Promise; -type LazyImportType = Record; +type NamedComponents = Record; /** * A utility function that enhances React.lazy() by allowing named imports. This function @@ -14,12 +14,15 @@ type LazyImportType = Record; * @see {https://github.com/facebook/react/issues/14603#issuecomment-736878172 React's discussion on named imports} * @see {https://github.com/JLarky/react-lazily/blob/main/src/core/lazily.ts react-lazily source code for a similar approach} */ -export const lazyImport = (loader: Loader) => { +export const lazyImport = (loader: Loader) => { return new Proxy({} as T, { get: (_target, name: ComponentName) => { return lazy(async () => { const module = await loader(); - return { default: module[name] as ComponentType }; + const Component = module[name] as ComponentType; + if (!Component) throw new Error(`Component ${name} not found`); + + return { default: Component }; }); }, });