External API Integration
Integrate timepicker with external time APIs for real-world applications
API Used
These examples use TimeAPI.io to fetch current time from different timezones. You can use any API that returns time data.
Basic - Fetch and Set Time
#Simple example: Fetch current time from API and set it to the timepicker on mount.
Advanced - Auto-Refresh with Events
#Advanced example: Auto-refresh time every 60 seconds with timezone selector, EventEmitter API, and error handling.
Multiple Timezones with Validation
#Complex example: Random timezone selection, 12h format, disabled night hours, and full EventEmitter integration.
Hours 1 AM - 5 AM are disabled. Fetches random timezone on mount.
Unsubscribe from an Event
#on() and once() return a function that removes the handler. Call it when the code that subscribed goes away, for example in a React effect cleanup.
Key Features Demonstrated
#- setValue() - Set time programmatically from API
- EventEmitter API - Listen to events with on(), once(), off()
- Callback options - onConfirm, onUpdate, onError handlers
- Grouped options - clock, ui, labels, behavior, callbacks
- Auto-refresh - Update time periodically with setInterval
- Timezone support - Fetch time from different timezones
- Validation - disabledTime for restricted hours
- Error handling - Proper try/catch with user feedback
- Cleanup - Destroy picker and clear intervals in useEffect
Production Tips
- Always handle API errors gracefully with try/catch
- Clean up intervals and destroy pickers in React useEffect cleanup
- Use EventEmitter API for better TypeScript support
- Consider rate limiting and caching for frequent API calls
- Validate time data from external APIs before setting