-
-
Notifications
You must be signed in to change notification settings - Fork 146
Expand file tree
/
Copy pathserver-only.tsx
More file actions
29 lines (27 loc) · 784 Bytes
/
server-only.tsx
File metadata and controls
29 lines (27 loc) · 784 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
import * as React from "react";
import { useHydrated } from "./use-hydrated.js";
type Props = {
/**
* You are encouraged to add a fallback that is the same dimensions
* as the server rendered children. This will avoid content layout
* shift which is disgusting
*/
children(): React.ReactNode;
fallback?: React.ReactNode;
};
/**
* Render the children only before the JS has loaded client-side. Use an
* optional fallback component for once the JS has loaded.
*
* Example: Render a hidden input to identify if the user has JS.
* ```tsx
* return (
* <ServerOnly fallback={<FakeChart />}>
* {() => <Chart />}
* </ServerOnly>
* );
* ```
*/
export function ServerOnly({ children, fallback = null }: Props) {
return useHydrated() ? fallback : children();
}