useNow
Returns the current time as a Date. Starts from MosaicProvider's mount time when inside one, and only ticks when updateInterval is set.
Use for
- any current time read during render
- a countdown or relative time that ticks
- stopping a tick at a deadline with
until
Practices
| Guidance | Practice |
|---|---|
| Do | Pass until for a countdown, so the interval stops once the deadline passes. |
| Don’t | Call new Date() or Date.now() during render. Use this hook instead. |
Options
| Prop | Type | Description |
|---|---|---|
updateInterval | number | Milliseconds between ticks. No ticking when unset. |
until | number | A timestamp in milliseconds. Ticking stops once now reaches it. |
Examples
Tick every second until a deadline
const now = useNow({ updateInterval: 1000, until: expiresAt });