Skip to main content

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​

TrackingScreen.tsx
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();
FieldDescription
authorizationnotDetermined, whenInUse, always, denied, or restricted.
preciseLocationPrecise (full accuracy) location is granted.
hasForegroundWhen-In-Use or Always.
hasBackgroundAlwaysAlways only.
guidance.codeok, request_when_in_use, request_always, open_settings, or enable_precise.
guidance.messageCopy you can show next to the button.
requestWhenInUse / requestAlwaysCall the module permission prompts.
refreshRe-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,
});
OptionDefaultDescription
serverUrl—Used when startTracking() is called without an argument.
reconcileIntervalMs1000How often to re-read getStatus() so counters stay fresh when events are sparse. 0 disables.
reconcileOnForegroundtrueRefresh when the app becomes active.
ReturnDescription
statusFull Status snapshot.
trackingStateLast onTrackingStateChange event, or null.
lastSyncLast onSyncStatus event, or null.
isTrackingstatus.running || status.desired.
isReadyauthorization === 'always'.
isStarting / isStopping / isSyncingIn-flight flags for your buttons.
errorLast 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,
});
OptionDefaultDescription
enabledtrueSubscribe to onLocationUpdate.
maxItems20Cap on the in-memory list (newest first).
loadPersistedfalseSeed the list from getLocations() on mount.
persistedStatus'pending'pending, synced, rejected, or all.
persistedLimit20How 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);
HelperDescription
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, or getDiagnostics
  • 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.