I'm new to react. My problem is that after the fetch the state of allCards is being changed but not the state of currentCard and showCard.
const [showCard, setShowCard] = useState(false)
const [allCards, setallCards] = useState<CardProp[]>([]);
const [currentCard, setcurrentCard] = useState<CardProp>();
function getRandomCard() {
return allCards[Math.floor(Math.random() * allCards.length)];
}
function updateCard() {
setcurrentCard(getRandomCard());
}
const fetchCards = async () => {
const cardRequest = await fetch("/api/card", {
headers: {
"Content-Type": "application/json",
Authorization: token!,
},
});
console.log(cardRequest);
if (cardRequest.status === 200) {
const cardJSON = await cardRequest.json();
setallCards(cardJSON.cards);
}
};
useEffect(() => {
fetchCards();
if (allCards.length > 0) {
setcurrentCard(getRandomCard());
setShowCard(true);
}
}, []);