Skip to main content

useLocationTracking

Lightweight hook that only monitors whether location tracking is currently active. Use this when you need to display tracking status without managing the tracking lifecycle.

import { useLocationTracking } from '@gabriel-sisjr/react-native-background-location';

Platform: Android and iOS


Signature

function useLocationTracking(autoRefresh?: boolean): UseLocationTrackingResult

Parameters

ParameterTypeDefaultDescription
autoRefreshbooleantrueWhether to automatically check tracking status on mount

Return Value

Returns a UseLocationTrackingResult object.

PropertyTypeDescription
isTrackingbooleanWhether location tracking is currently active
tripIdstring | nullCurrent trip ID if tracking is active, null otherwise
refresh() => Promise<void>Manually re-check tracking status from the native module
isLoadingbooleanWhether a status check is in progress

Behavior Details

Mount Check

When autoRefresh is true (the default), the hook calls isTracking() on the native module when the component mounts and populates the state with the result.

Manual Refresh

Call refresh() to re-query the native module for the current tracking status. This is useful when you know tracking state may have changed externally (e.g., via a different component or screen).

Native Module Unavailability

If the native module is not available (e.g., running in a simulator), the hook logs a warning and reports { isTracking: false, tripId: null }.


Usage

Status badge

import { useLocationTracking } from '@gabriel-sisjr/react-native-background-location';

function StatusBadge() {
const { isTracking, tripId } = useLocationTracking();

return (
<View>
<Text>Status: {isTracking ? 'Tracking' : 'Stopped'}</Text>
{tripId && <Text>Trip: {tripId}</Text>}
</View>
);
}

Manual refresh

function TrackingIndicator() {
const { isTracking, refresh, isLoading } = useLocationTracking();

return (
<View>
<Text>{isTracking ? 'Active' : 'Inactive'}</Text>
<Button title="Refresh" onPress={refresh} disabled={isLoading} />
</View>
);
}

Without auto-refresh

function LazyStatus() {
const { isTracking, refresh } = useLocationTracking(false);

// Status won't be checked until refresh() is called
useEffect(() => {
// Check status when some condition is met
if (someCondition) {
refresh();
}
}, [someCondition, refresh]);

return <Text>{isTracking ? 'On' : 'Off'}</Text>;
}

When to Use

Use useLocationTracking when you:

  • Only need to read tracking status (not start/stop)
  • Want a lightweight hook with minimal overhead
  • Need a status indicator in a component far from the tracking controller

Use useBackgroundLocation instead when you need to start/stop tracking, manage trip data, or access stored locations.

FeatureuseLocationTrackinguseBackgroundLocation
Read tracking statusYesYes
Start/stop trackingNoYes
Access locationsNoYes
Trip managementNoYes
Lifecycle callbacksNoYes
Bundle size impactMinimalLarger