React Hooks Guide
This guide shows how to use React hooks for monitoring runtime device properties in your React Native application.
Overview
React hooks read device getters after mount and poll for updates. They clear their timers when the component unmounts. These hooks do not subscribe to native events or guarantee immediate notifications.
Benefits:
- Automatic cleanup on component unmount
- Reactive updates when device state changes
- Familiar React hook pattern
- TypeScript support out of the box
Installation
The hooks are included with react-native-nitro-device-info:
Update intervals and initial values
Timers depend on JavaScript scheduling and can be delayed in the background. A hook does not report a change at the exact moment it occurs.
The examples use root imports. Headphone hooks from /compat instead return { loading, result }; see the Migration Guide.
Quick Examples
Battery Monitoring
Monitor battery level with automatic re-renders:
Low Battery Alerts
Show warnings when battery is low:
Headphone Detection
Adapt your UI based on audio output:
Brightness Monitoring (iOS)
Track screen brightness changes:
Complete Example: Device Monitor Dashboard
Troubleshooting
Hook returns null indefinitely
Check that the native module is properly linked:
Updates not received
Ensure the app is in the foreground to receive updates from hooks.
Brightness returns -1
This is expected on Android - brightness monitoring is iOS-only.
See Also
- Hooks API Reference - Complete hook signatures and types
- Migration Guide - Migrating from react-native-device-info
- Basic Usage Examples - More usage examples
