I have the following function that refreshes my states
const refreshAfterCreate = async () => {
try {
const res = await fetch(process.env.NEXT_PUBLIC_MICROSERVICE_COMPANY_URL + '/companies', {method: 'GET'});
const data = await res.json();
await setCompanies(data);
} catch (error) {
console.error('Error al realizar el fetch:', error);
}
try {
const res = await fetch(process.env.NEXT_PUBLIC_MICROSERVICE_SERVICE_URL + '/services', {method: 'GET'});
const data = await res.json();
await setServices(data);
} catch (error) {
console.error('Error al realizar el fetch:', error);
}
};
After calling the function with await, I console.log(companies) and console.log(services) and is the previous data.
I tried adding these useEffects
useEffect(() => {
console.log('Companies Updated', companies);
}, [companies]);
useEffect(() => {
console.log('Services Updated', services);
}, [services]);
These work since it logs the updated value. When I call the function above and log everything it updates the values first but then goes back to the previous state, this image helps understand.
How can I fix this?