Skip to content

Commit 5d31cf0

Browse files
committed
feat: 🎸 support useBattery hook on server side
1 parent 0e9894d commit 5d31cf0

File tree

1 file changed

+3
-3
lines changed

1 file changed

+3
-3
lines changed

src/useBattery.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -25,8 +25,8 @@ type UseBatteryState =
2525
| { isSupported: true; fetched: false } // battery API supported but not fetched yet
2626
| BatteryState & { isSupported: true; fetched: true }; // battery API supported and fetched
2727

28-
const nav: NavigatorWithPossibleBattery = navigator;
29-
const isBatteryApiSupported = nav && typeof nav.getBattery !== 'undefined';
28+
const isBatteryApiSupported =
29+
typeof navigator === 'object' && typeof (navigator as NavigatorWithPossibleBattery).getBattery === 'function';
3030

3131
function useBatteryMock(): UseBatteryState {
3232
return { isSupported: false };
@@ -85,7 +85,7 @@ function useBattery(): UseBatteryState {
8585
React.useEffect(() => {
8686
isMounted.current = true;
8787

88-
nav.getBattery!().then((bat: BatteryManager) => {
88+
(navigator as NavigatorWithPossibleBattery).getBattery!().then((bat: BatteryManager) => {
8989
battery.current = bat;
9090

9191
bindBatteryEvents();

0 commit comments

Comments
 (0)