Experimental Hooks
useSuspenseNavigatorBattery
About
⚠️ Experimental Hook: This hook may be removed or significantly changed in any release without notice.
A Suspense-enabled hook for getting battery status information from the Navigator Battery API with real-time updates. This hook suspends while the API is fetching data and must be wrapped in a Suspense boundary. It automatically listens for battery status changes and updates the component accordingly.
Examples
Basic Usage
With Error Boundary
Battery Level Indicator with Real-time Updates
Real-time Updates
The hook automatically listens for battery status changes using the BatteryManager
event listeners:
chargingchange
- Fired when the battery charging state changeslevelchange
- Fired when the battery level changeschargingtimechange
- Fired when the battery charging time changesdischargingtimechange
- Fired when the battery discharging time changes
Your components will automatically re-render when any of these events occur, providing live updates without any additional setup.
Arguments
None. This hook takes no parameters.
Return Value
Returns a BatteryManager
object with the following properties:
Property | Type | Description |
---|---|---|
charging | boolean | Whether the battery is charging |
chargingTime | number | Time remaining in seconds until the battery is charged (Infinity if unknown) |
dischargingTime | number | Time remaining in seconds until the battery is discharged (Infinity if unknown) |
level | number | Battery charge level (0.0 to 1.0) |
Browser Support
- Chrome/Edge: ✅ Supported (with HTTPS requirement)
- Firefox: ❌ Removed for privacy reasons
- Safari: ❌ Not supported
- Mobile browsers: Limited support
⚠️ Important Notes:
- Requires HTTPS in most browsers
- API may be restricted or removed for privacy reasons
- Always wrap in error boundaries for production use
- Consider this an experimental API that may change or be removed