I'm trying to set a state variable inside a function, but it doesn't get set immediately.
It eventually gets set, but I want to interact with the data inside the function as soon as the state is set.
code:
const [addresses,setAddresses] = useState<any[]>([]);
const fetchData = async () =>{
try{
const addresses = await fetchAddresses(data);
setAddresses(addresses);
console.log(addresses)
//code that interacts with addresses state variable
}
catch(err){
console.log(err);
}
}
useEffect(()=>{
if(emptyArray){
fetchData();
}
},[]);
Any help is appreciated, thanks.