forked from thirdweb-dev/js
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathClientOnly.tsx
42 lines (34 loc) · 806 Bytes
/
ClientOnly.tsx
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
33
34
35
36
37
38
39
40
41
42
"use client";
import { cn } from "@/lib/utils";
import { type ReactNode, useEffect, useState } from "react";
interface ClientOnlyProps {
/**
* Use this to server render a skeleton or loading state
*/
ssr: ReactNode;
className?: string;
children: ReactNode;
}
export const ClientOnly: React.FC<ClientOnlyProps> = ({
children,
ssr,
className,
}) => {
const hasMounted = useIsClientMounted();
if (!hasMounted) {
return <> {ssr} </>;
}
return (
<div className={cn("fade-in-0 animate-in duration-300", className)}>
{children}
</div>
);
};
function useIsClientMounted() {
const [hasMounted, setHasMounted] = useState(false);
// eslint-disable-next-line no-restricted-syntax
useEffect(() => {
setHasMounted(true);
}, []);
return hasMounted;
}