Quick start
Read device details
getDeviceDetails() returns values that stay the same while the app runs. The native side reads them on the first call and caches them, so calling it again is cheap.
import { DeviceInfo } from 'nitro-device-info'
const { modelName, deviceType, systemName, systemVersion, appVersion } =
DeviceInfo.getDeviceDetails()
Observe the battery
getPowerState() returns the current state. To follow changes, add a listener and remove it when the component unmounts:
import { useEffect, useState } from 'react'
import { DeviceInfo } from 'nitro-device-info'
export function Battery() {
const [power, setPower] = useState(() => DeviceInfo.getPowerState())
useEffect(() => {
const subscription = DeviceInfo.addOnPowerStateChangedListener(setPower)
return () => subscription.remove()
}, [])
// ...render power.batteryLevel, power.batteryState, power.lowPowerMode
}
Resolve the async values
isLocationEnabled() and getUserAgent() return promises, because the native calls may block:
const locationEnabled = await DeviceInfo.isLocationEnabled()
const userAgent = await DeviceInfo.getUserAgent()
Read values that change
getUsedMemory(), getFontScale(), getVendorId(), isHeadphonesConnected() and isPasscodeSet() read the current value on every call. Call them again when you need a fresh reading; none of them has a listener.
const usedMegabytes = DeviceInfo.getUsedMemory() / 1024 ** 2
const fontScale = DeviceInfo.getFontScale()
Next
- Power state: when the listener fires and what it costs.
- Platform differences.
- Example app: a screen that shows every value.