-
-
Notifications
You must be signed in to change notification settings - Fork 146
Expand file tree
/
Copy pathuse-hydrated.ts
More file actions
31 lines (29 loc) · 831 Bytes
/
use-hydrated.ts
File metadata and controls
31 lines (29 loc) · 831 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
import { useSyncExternalStore } from "react";
function subscribe() {
// biome-ignore lint/suspicious/noEmptyBlockStatements: Mock function
return () => {};
}
/**
* Return a boolean indicating if the JS has been hydrated already.
* When doing Server-Side Rendering, the result will always be false.
* When doing Client-Side Rendering, the result will always be false on the
* first render and true from then on. Even if a new component renders it will
* always start with true.
*
* Example: Disable a button that needs JS to work.
* ```tsx
* let hydrated = useHydrated();
* return (
* <button type="button" disabled={!hydrated} onClick={doSomethingCustom}>
* Click me
* </button>
* );
* ```
*/
export function useHydrated() {
return useSyncExternalStore(
subscribe,
() => true,
() => false,
);
}