React hooks
Most apps only need three hooks. Paste the screen below, grant Always location, and you are tracking. For HTTP config, geofences, schedules, the on-device queue, odometer, or diagnostics, call the same LocationTracker methods from anywhere. How much control you take is up to you. The hooks call that module; dropping down does not leave the supported API.
Paste this screen
import React from 'react';
import {Button, SafeAreaView, Text, View} from 'react-native';
import {
useLocationTracking,
useLocationUpdates,
usePermissions,
} from 'react-native-background-location-pro-eval';
const SERVER_URL = 'https://api.example.com';
export function TrackingScreen() {
const permissions = usePermissions();
const {
isTracking,
isStarting,
isStopping,
status,
startTracking,
stopTracking,
syncNow,
} = useLocationTracking({serverUrl: SERVER_URL});
const {latestLocation, locations} = useLocationUpdates({maxItems: 20});
return (
<SafeAreaView style={{padding: 24, gap: 12}}>
<Text>{permissions.guidance.message}</Text>
{permissions.guidance.code === 'request_when_in_use' && (
<Button title="Allow location" onPress={permissions.requestWhenInUse} />
)}
{permissions.guidance.code === 'request_always' && (
<Button title="Allow Always" onPress={permissions.requestAlways} />
)}
<Button
title={isTracking ? 'Stop tracking' : 'Start tracking'}
disabled={isStarting || isStopping}
onPress={() => (isTracking ? stopTracking() : startTracking())}
/>
<Button title="Sync now" onPress={() => syncNow()} />
<Text>
{status.pending} pending · {status.synced} synced
{latestLocation
? ` · ${latestLocation.latitude.toFixed(5)}, ${latestLocation.longitude.toFixed(5)}`
: ''}
</Text>
<Text>{locations.length} points in this session</Text>
</SafeAreaView>
);
}
When that feels solid in a debug build, license the source for the same API with store-ready setup, the ingestion server, and the live map.
usePermissions()
Permission state and a short next-step code for your UI.
import {usePermissions} from 'react-native-background-location-pro-eval';
const {
authorization,
preciseLocation,
hasForeground,
hasBackgroundAlways,
guidance,
error,
refresh,
requestWhenInUse,
requestAlways,
} = usePermissions();
| Field | Description |
|---|---|
authorization | notDetermined, whenInUse, always, denied, or restricted. |
preciseLocation | Precise (full accuracy) location is granted. |
hasForeground | When-In-Use or Always. |
hasBackgroundAlways | Always only. |
guidance.code | ok, request_when_in_use, request_always, open_settings, or enable_precise. |
guidance.message | Copy you can show next to the button. |
requestWhenInUse / requestAlways | Call the module permission prompts. |
refresh | Re-read status after the user returns from Settings. |
useLocationTracking(options?)
Start, stop, sync, and a live status snapshot.
import {useLocationTracking} from 'react-native-background-location-pro-eval';
const tracking = useLocationTracking({
serverUrl: 'https://api.example.com',
reconcileIntervalMs: 1000, // set 0 to disable
reconcileOnForeground: true,
});
| Option | Default | Description |
|---|---|---|
serverUrl | — | Used when startTracking() is called without an argument. |
reconcileIntervalMs | 1000 | How often to re-read getStatus() so counters stay fresh when events are sparse. 0 disables. |
reconcileOnForeground | true | Refresh when the app becomes active. |
| Return | Description |
|---|---|
status | Full Status snapshot. |
trackingState | Last onTrackingStateChange event, or null. |
lastSync | Last onSyncStatus event, or null. |
isTracking | status.running || status.desired. |
isReady | authorization === 'always'. |
isStarting / isStopping / isSyncing | In-flight flags for your buttons. |
error | Last thrown error from start/stop/sync, or null. |
startTracking(serverUrl?) | Resolves to OpResult. Needs a URL from the argument, options, or status. |
stopTracking() / syncNow() | Same as the module; in-flight calls are shared so double taps are safe. |
refreshStatus() | Force a getStatus() read. |
useLocationUpdates(options?)
Latest fix and a short in-memory list for maps and lists.
import {useLocationUpdates} from 'react-native-background-location-pro-eval';
const {latestLocation, locations, error, clear} = useLocationUpdates({
enabled: true,
maxItems: 20,
loadPersisted: false,
persistedStatus: 'pending',
persistedLimit: 20,
});
| Option | Default | Description |
|---|---|---|
enabled | true | Subscribe to onLocationUpdate. |
maxItems | 20 | Cap on the in-memory list (newest first). |
loadPersisted | false | Seed the list from getLocations() on mount. |
persistedStatus | 'pending' | pending, synced, rejected, or all. |
persistedLimit | 20 | How many stored rows to load when loadPersisted is true. |
Helpers
Useful before you call startTracking, or when you want one line of guidance without the hooks.
import {
getTrackingStartContext,
getTrackingReadiness,
buildTrackingStartGuidance,
isPermissionGranted,
getActionableErrorMessage,
} from 'react-native-background-location-pro-eval';
const ctx = await getTrackingStartContext('https://api.example.com');
// ctx.urlValidation.ok, ctx.readiness.ready, ctx.status
const guidance = buildTrackingStartGuidance(ctx.readiness);
const label = getActionableErrorMessage(error);
| Helper | Description |
|---|---|
getTrackingStartContext(serverUrl) | Status + URL validation + readiness in one call. |
getTrackingReadiness(status) | {ready, reason} from a status snapshot. |
buildTrackingStartGuidance(readiness) | Human-readable next step. |
isPermissionGranted(status) | When-In-Use or Always. |
getActionableErrorMessage(error) | Formats {code, message} errors for UI. |
When to use the imperative API
Stay on the hooks for screens. Reach for LocationTracker when you need:
- Custom HTTP path, headers, or cellular sync policy (
setHttpConfig) - Geofences or a weekly schedule
- Queue inspection (
getLocations,getQueueStats,destroyLocations) - Odometer, one-shot
getCurrentPosition, orgetDiagnostics - Wiring into a store, a headless task, or your own event bus
Both paths use the same module. Pick the level of control your feature needs.