After getting the documents I want and storing as objects in an array shown here
const appealsRef = collection(db, "appeals");
const today = new Date();
const getAppeals = async (logicalOp) => {
const q = query(appealsRef, where("toDate", logicalOp, convertDateToTimestamp(today)))
const querySnapshot = await getDocs(q);
let dummyAppeals = [];
querySnapshot.docs.map((doc) => {
const docData = { ...doc.data() };
getOrgByDocID(docData.orgDocID)
.then((res) => {
const appealData = {
...docData,
orgName : res.orgName,
orgAddress : res.orgAddress
}
dummyAppeals.push(appealData)
})
});
return dummyAppeals
}
I would like to set the results in state variables currentAppeals and pastAppeals based on the logicalOp given in useEffect and then set it into another state variable array appeals using setAppeals; this is because depending on what the user clicks on which is past or present I would like to set the appeals array to currentAppeals if present is clicked or pastAppeals if past is clicked.
The code involved :
const [ currentAppeals, setCurrentAppeals ] = useState([]);
const [ appeals, setAppeals ] = useState([]);
useEffect(() => {
getAppeals(">=")
.then((res) => {
setCurrentAppeals(res);
setAppeals(currentAppeals.map((appeal, i) => {
return (
<AppealCard
appealID={appeal.appealID}
from={appeal.fromDate}
to={appeal.toDate}
orgName={appeal.orgName}
orgAddress={appeal.orgAddress}
outcome={appeal.outcome}/>
)
}));
});
}, []);
And then I will display the appeals in the react component here :
<div className='view-appealsBody'>
{
appeals
}
</div>
But the appeals are not set after setAppeals in useEffect().