useNow
v0.53.0, changelogThe clock, read again at an interval, and null until the page has hydrated, so a server render and the browser's first one never disagree about the time.
Install
npx shadcn add @syncrobotic/use-now Overview
When to use
- "Updated 12 s ago", a timeout, an alarm flood's window.
- Stale data: how long since the last message.
Use something else
- A time of day to show: format the value with Clock time.
Usage rules
- The clock for "12 s ago", for what has timed out, for a flood window:
useNow(intervalMs), null on the server and until the page hydrates, so the two renders agree. - Render nothing time-based (or a placeholder) while it is null, never
Date.now()in render.
API
What the file exports. The source is in the Source sheet above.
- useClientValue<T>(read: () => T, server: T): T
- A value only the browser can read and that does not change while the page is open (the platform, WebGL support).
- useHydrated(): boolean
- False on the server and while the page hydrates, true after: for what only the browser can draw.
- useNow(intervalMs: number | null = 1000): number | null
- The clock, read again at an interval, and null until the page has hydrated, so a server render and the browser's first one never disagree about the time.
Changelog
- 0.53.02026-11-27Its doc opens with its description, word for word; the code is unchanged.
- 0.52.02026-11-27Adds useClientValue and useHydrated, shared instead of per-component copies.
- 0.51.02026-11-27Stable: its API is a standard one the kit styles, unchanged for ten releases, with unit tests. It changes only in a major release from here. Comments trimmed to what they need to say; the code is unchanged.