-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathclient.tsx
More file actions
32 lines (29 loc) · 780 Bytes
/
client.tsx
File metadata and controls
32 lines (29 loc) · 780 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
import { lazy as oldLazy, useEffect, useState } from "react";
/** @deprecated use lazy with suspense */
export function NoSSR({
children,
fallback,
}: {
children: React.ReactNode;
fallback?: React.ReactNode;
}) {
const [state, setState] = useState<React.ReactNode>(fallback);
useEffect(() => setState(children), [children]);
return <>{state}</>;
}
export class ClientOnlyError extends Error {
constructor() {
super("client only");
}
}
export function lazy<T>(
importFunc: () => Promise<{ default: React.ComponentType<T> }>
): React.ComponentType<T> {
const LazyComponent = oldLazy(importFunc);
return (props: any) => {
if (typeof window === "undefined") {
throw new ClientOnlyError();
}
return <LazyComponent {...props} />;
};
}