expo-keep-awake
Version: 55.0.3
Provides a React component that prevents the screen from sleeping when rendered. It also exposes static methods to control the behavior imperatively.
Installation
npx expo install expo-keep-awake
Usage
Component-Based
import { KeepAwake } from 'expo-keep-awake';
import { View, Text } from 'react-native';
function VideoPlayer() {
return (
<View>
<KeepAwake />
<Text>Screen will stay awake while this component is mounted</Text>
</View>
);
}
Hook-Based
import { useKeepAwake } from 'expo-keep-awake';
import { View, Text } from 'react-native';
function VideoPlayer() {
useKeepAwake();
return <Text>Screen will stay awake</Text>;
}
Imperative API
import { activateKeepAwake, deactivateKeepAwake } from 'expo-keep-awake';
// Activate
activateKeepAwake();
// Deactivate
deactivateKeepAwake();
API Reference
Component
Renders a component that prevents screen from sleepingAutomatically activates when mounted and deactivates when unmounted.
Hook
Hook that keeps screen awake while component is mounteduseKeepAwake('video-player');
Optional tag parameter allows multiple independent keep-awake instances.
Methods
Activates keep-awake modeimport { activateKeepAwake } from 'expo-keep-awake';
activateKeepAwake('my-tag');
deactivateKeepAwake(tag?)
Deactivates keep-awake modeimport { deactivateKeepAwake } from 'expo-keep-awake';
deactivateKeepAwake('my-tag');
Examples
Video Player
import { useKeepAwake } from 'expo-keep-awake';
import { Video } from 'expo-av';
import { useState } from 'react';
function VideoPlayer({ source }) {
const [isPlaying, setIsPlaying] = useState(false);
// Keep screen awake only while video is playing
if (isPlaying) {
useKeepAwake();
}
return (
<Video
source={source}
onPlaybackStatusUpdate={(status) => {
setIsPlaying(status.isPlaying ?? false);
}}
/>
);
}
Navigation Screen
import { KeepAwake } from 'expo-keep-awake';
import { View, Text } from 'react-native';
import MapView from 'react-native-maps';
function NavigationScreen() {
return (
<View style={{ flex: 1 }}>
<KeepAwake />
<MapView style={{ flex: 1 }} />
<Text>Screen stays on during navigation</Text>
</View>
);
}
Conditional Keep Awake
import { useKeepAwake } from 'expo-keep-awake';
import { View, Switch, Text } from 'react-native';
import { useState } from 'react';
function SettingsScreen() {
const [keepScreenOn, setKeepScreenOn] = useState(false);
if (keepScreenOn) {
useKeepAwake();
}
return (
<View>
<Text>Keep Screen On</Text>
<Switch value={keepScreenOn} onValueChange={setKeepScreenOn} />
</View>
);
}
Imperative Control
import { activateKeepAwake, deactivateKeepAwake } from 'expo-keep-awake';
class MediaPlayer {
play() {
activateKeepAwake('media-player');
// Start playback...
}
pause() {
deactivateKeepAwake('media-player');
// Pause playback...
}
stop() {
deactivateKeepAwake('media-player');
// Stop playback...
}
}
import { activateKeepAwake, deactivateKeepAwake } from 'expo-keep-awake';
import { useEffect } from 'react';
function VideoPlayer() {
useEffect(() => {
activateKeepAwake('video');
return () => deactivateKeepAwake('video');
}, []);
return <Video />;
}
function AudioPlayer() {
useEffect(() => {
activateKeepAwake('audio');
return () => deactivateKeepAwake('audio');
}, []);
return <Audio />;
}
// Both can be active independently
function App() {
return (
<>
<VideoPlayer />
<AudioPlayer />
</>
);
}
Reading Mode
import { useKeepAwake } from 'expo-keep-awake';
import { ScrollView, Text } from 'react-native';
function Article({ content }) {
// Keep screen on while reading
useKeepAwake('reading');
return (
<ScrollView>
<Text>{content}</Text>
</ScrollView>
);
}
Camera Preview
import { KeepAwake } from 'expo-keep-awake';
import { CameraView } from 'expo-camera';
function CameraScreen() {
return (
<>
<KeepAwake />
<CameraView style={{ flex: 1 }} />
</>
);
}
TypeScript
import {
KeepAwake,
useKeepAwake,
activateKeepAwake,
deactivateKeepAwake
} from 'expo-keep-awake';
// Hook with optional tag
useKeepAwake('my-tag');
// Imperative with tag
activateKeepAwake('my-tag');
deactivateKeepAwake('my-tag');
| Platform | Supported |
|---|
| iOS | ✅ |
| Android | ✅ |
| Web | ✅ |
How It Works
- iOS: Sets
UIApplication.shared.isIdleTimerDisabled = true
- Android: Adds
FLAG_KEEP_SCREEN_ON to the window
- Web: Uses Wake Lock API or NoSleep.js fallback
Best Practices
- Use Component/Hook: Prefer
<KeepAwake /> or useKeepAwake() for automatic cleanup
- Clean Up: Always deactivate when no longer needed to save battery
- Use Tags: Use unique tags for multiple independent instances
- Conditional Usage: Only activate when actually needed (e.g., during video playback)
- User Control: Consider letting users disable keep-awake in settings
Keeping the screen on drains battery faster. Only use when necessary for user experience.
Resources