Currently I am fetching some data and I save the response via useState
. The response is saved but the problem is the second fetch takes place before the state was saved making that value undefined
.
I tried to do like this:
useEffect(() => {
fetchTrackingData().finally(() => {
console.log("Destination", destination.coordinates);
fetchDriverLocation().finally(() => {
setLoading(false);
});
});
}, []);
Basically fetchTrackingData
function has destination coordinates which I save with:
const [destination, setDestination] = useState('');
In the fetchTrackingData
function I am setting the state after receiving the response. After fetchTrackingData
is done fetching and it has saved state of destination
I then use finally()
method and then call the second fetch which is fetchDriverLocation
but it has destination
undefined and even I logged to see if the value gets saved after finally()
but it is undefined
in log as well.
How can I store the value from one API in that API call and then use that value in another API call?