Overview
Legend-State provides pre-configured time observables that automatically update at regular intervals. These are useful for displaying current time, implementing time-based UI updates, or scheduling periodic actions.Available Observables
currentTime
An observable that contains the current time and updates every minute.currentDay
An observable that contains the current day (with time set to midnight) and updates at midnight.Usage in React
Display Current Time
currentTime updates.
Display Relative Time
Display Current Date
Advanced Usage
Computed Time Values
Time-Based Conditions
Listen to Time Changes
Implementation Details
The time observables are implemented as follows:- Initial sync: Both observables are initialized with the current time when the module loads
- currentTime: Updates every 60 seconds (1 minute) using
setInterval - currentDay: Checks on each
currentTimeupdate if the day has changed, and updates accordingly - First update: The first update happens at the next full minute (e.g., if initialized at 10:23:45, first update at 10:24:00)
- Time clearing:
currentDayhas time set to00:00:00.000usingsetHours(0, 0, 0, 0)
Performance Considerations
Since these observables update automatically, components that observe them will re-render periodically:
currentTime: Component re-renders every minutecurrentDay: Component re-renders at midnight