I'm trying to get data from an API call and update context with the result.
I have this function to get data, this should return an array:
export async function fetchAircraft(bearerToken) {
const response = await axios.get(apiURL + 'User-getAircraft',
{headers: {
'Authorization': `Bearer ${bearerToken}`,
'content-type': 'application/json'
}}
);
console.log('http resp: ',response.data);
return response.data;
}
I'm calling the fetchAircraft function from my dashboard:
useEffect(() => {
async function getAircraft() {
const aircraft = await fetchAircraft(userContext.idToken);
console.log('dashboard: ',aircraft);
userContext.setAircraft(aircraft);
}
getAircraft();
navigation.setOptions({
headerRight: () => (<AircraftSelector/>)
})
},[])
When my dashboard loads and I view the logs, the console log in the fetchAircraft function only shows a single item in the array (as does the console log in useEffect. If I comment out the userContext.setAircraft call, the console logs both show the correct array with all items included. I cannot figure out why the call to set aircraft in the context is limiting what is returned by the fetchAircraft function.