I would like to know if there is a way to execute a function in expo task manager, I have tried severally but the app keeps crashing with an undefined error. Below is my sample code
I am working with expo task manager to fetch location in background. It works, however I am trying to pass a props from my redux into it to execute a function but I keep getting undefined object. Below is my sample code, I would appreciate if someone can point me to the right direction.
import React from 'react';
import { Text, TouchableOpacity } from 'react-native';
import * as TaskManager from 'expo-task-manager';
import * as Location from 'expo-location';
import { connect } from 'react-redux';
const LOCATION_TASK_NAME = 'background-location-task';
export default class Component extends React.Component {
onPress = async () => {
await Location.startLocationUpdatesAsync(LOCATION_TASK_NAME, {
accuracy: Location.Accuracy.Balanced,
timeInterval: 10000,
});
};
showLocation = (location) => {
const { activeList } = this.props;
console.log('current location:', location);
firebaseApi.update({ activeList, location });
//I need it to update my firebase database
}
render() {
return (
<TouchableOpacity onPress={this.onPress} style={{marginTop: 100}}>
<Text>Enable background location</Text>
</TouchableOpacity>
);
}
}
TaskManager.defineTask(LOCATION_TASK_NAME, async ({ data, error }) => {
if (error) {
console.log(error);
return;
}
if (data) {
const { locations } = data;
const latitude = locations[0].coords.latitude;
const longitude = locations[0].coords.longitude;
const loc = { latitude, longitude };
Component.showLocation(loc);
// Storing Received Lat & Long to DB by logged In User Id
// console.log("Received new locations for user = ", userId, locations);
}
});
const mapStateToProps = (state) => ({
activeList: state.userDetail.activeList,
});