Skip to content

Commit 4a086f4

Browse files
committed
add lazy
1 parent a5e752b commit 4a086f4

File tree

1 file changed

+14
-1
lines changed

1 file changed

+14
-1
lines changed

client.tsx

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
1-
import { useEffect, useState } from "react";
1+
import { lazy as oldLazy, useEffect, useState } from "react";
22

3+
/** @deprecated use lazy with suspense */
34
export function NoSSR({
45
children,
56
fallback,
@@ -11,3 +12,15 @@ export function NoSSR({
1112
useEffect(() => setState(children), [children]);
1213
return <>{state}</>;
1314
}
15+
16+
export function lazy<T>(
17+
importFunc: () => Promise<{ default: React.ComponentType<T> }>
18+
): React.ComponentType<T> {
19+
const LazyComponent = oldLazy(importFunc);
20+
return (props: any) => {
21+
if (typeof window === "undefined") {
22+
throw new Error("client only");
23+
}
24+
return <LazyComponent {...props} />;
25+
};
26+
}

0 commit comments

Comments
 (0)