Using expo-taskmanager with react context + hooks

Please provide the following:

  1. SDK Version: 38.0.9
  2. Platforms(Android/iOS/web/all): Android, iOS

Ok. So I in my screen, I need to dispatch the location to location context then in the component, a useffect will trigger when the location latitude and longitude changes and send the update to my server using websockets.

import React, { useCallback, useEffect, useState, useRef } from 'react';
import * as TaskManager from 'expo-task-manager';
import { useConnection } from '../../contexts/connection';

const
	taskNames = {
		driverLocationTracking: 'DRIVER_LOCATION_TRACKING',
	}
;

const { locationDispatch } = useConnection(); // ? How do I get the task to update the context

const Driver = props => {
	const { locationState, locationDispatch, getSocket, socketId, socketConnected, socketEmit, lastNotification } = useConnection();

	const dispatchLocation = ({ heading, latitude, longitude }) => {
		locationDispatch({
			type: actionTypes.location.SET_CURRENT_LOCATION,
			payload: {
				location: {
					heading,
					latitude,
					longitude,
				},
				// speed?
			},
		});
	};

	const handleUserLocationChange = ({ nativeEvent }) => { // react-native-maps
		if(getUser() && socketConnected()) {
			//console.log(locationState.address, locationState.countryCode, locationState.geocode);
			const
				{ coordinate } = nativeEvent
			;
			if(coordinate) {
				dispatchLocation(coordinate);
			}
		}
	};

const _handleAppStateChange = async nextAppState => {
		//console.log(appState, 'nextAppState:', nextAppState);
		switch(nextAppState) {
			case 'active':
				const
					status = await Location.hasStartedLocationUpdatesAsync(taskNames.driverLocationTracking)
				;
				if(status) {
					Location.stopLocationUpdatesAsync(taskNames.driverLocationTracking);
				}
				break;
			case 'background':
				//console.log('isAvailableForRiding:', isAvailableForRiding(), availability || Boolean(getRideStage().main), availability,  Boolean(getRideStage().main));
				if(isAvailableForRiding()) {
					Location.startLocationUpdatesAsync(taskNames.driverLocationTracking, {
						accuracy: Location.Accuracy.Balanced,
						timeInterval: 3500,
						distanceInterval: 0.5,
						foregroundService: {
							notificationTitle: 'BeamX Driving',
							notificationBody: 'Listening for notifications',
							notificationColor: theme.colors.secondary,
						},
						pausesUpdatesAutomatically: false,
						activityType: Location.ActivityType.AutomotiveNavigation,
						showsBackgroundLocationIndicator: true,
					});
				}
				break;
		}
	};

	return (null);
};

TaskManager.defineTask(taskNames.driverLocationTracking, ({ data: { locations }, error }) => {
	//console.log('Background update');
	if(error) {
		// check `error.message` for more details.
		console.log(error.message);
		return;
	}
	//console.log('Received new locations', locations);
	//console.log(`[Background location]: socketConnected: ${socketConnected()}. getSocket: ${getSocket()}. getUser: ${getUser()}. isAvailableForRiding: ${isAvailableForRiding()}`);
	if(locations && locations.length) {
		//console.log(locationState.address, locationState.countryCode, locationState.geocode);
		const
			{ coords } = locations[0]
		;
		//console.log('coords: ', locations);
		if(coords) {
			locationDispatch({
				type: actionTypes.location.SET_CURRENT_LOCATION,
				payload: {
					location: {
						heading: coords.heading,
						latitude: coords.latitude,
						longitude: coords.longitude,
					},
					// speed?
				},
			});
			//console.log('Background update');
		}
	}
});

I’m trying to figure out how to over come this, how do I get the task to atleast update the context? If I get the context updated the component can do the rest.

This topic was automatically closed 30 days after the last reply. New replies are no longer allowed.