I am updating a state from first API call and I want this state value to set another state from next API call.
But even though isSelected State is being set as true from first API, in fetchAllQuotes it is coming as false.
const [isOptionEnabled, setIsOptionEnabled] = useState<boolean>(false);
const [isSelected, setIsSelected] = useState<boolean>(false);
useEffect(() => {
fetchAllAPICalls();
}, []);
const fetchAllAPICalls = async () => {
await fetchAllSelections();
await fetchAllQuotes();
};
const fetchAllSelections = async () => {
const url = getUrlForSelections();
const params = getParamsForSelections();
let selected = false;
await AXIOS_GET(url,params).then((res) => {
res.data.forEach((item)=>{
if(item.isSelected) selected = true;
}
setIsSelected(selected);
}
}
const fetchAllQuotes = async () => {
const url = getUrlForQuotes();
const params = getParamsForQuotes();
let isPresent = false;
await AXIOS_GET(url,params).then((res) => {
res.data.forEach((item)=>{
//doSomething and set isPresent = true;
}
setIsEnabled(isPresent && isSelected);
}
}