I have two tables in Firebase: Vouchers & ClaimedVouchers. I am trying to display the vouchers that do not appear in the ClaimedVouchers table. So, I have a query that gets all of the vouchers, then another that checks if they're claimed and if it's claimed or not the function should return either true or false:
This is isClaimedAPI.js - checks if voucher is in ClaimedVoucher table - returns true/false
export default (voucher, user) => {
const [result, setResult] = useState(false);
async function isClaimed() {
var db = Firebase.firestore();
console.log("voucher");
await db
.collection("ClaimedVoucher")
.where("voucherID", "==", voucher)
.where("userID", "==", user)
.get()
.then(function (querySnapshot) {
if (querySnapshot.empty === false) {
result = true;
} else {
result = false;
}
})
.catch(function (error) {
console.log("Error getting documents: ", error);
});
console.log("This is result: ", result);
return result;
}
useEffect(() => {
isClaimed().then((result) => setResult(result));
}, []);
return result;
};
The query works fine and I can get the values I want from it.
In my main function, I first get the list of vouchers that I want to check that they're claimed and store them in a list. Then I use a map function to return the ones that haven't been claimed. This is where my question arises, how do I return the jsx for the vouchers that haven't been claimed based off the result of the firebase query?
var user = Firebase.auth().currentUser;
var uid = user.uid;
const [getVouchers, voucherList, errorMessage] = getVouchersAPI(ID); //List of vouchers to be checked
return(
<ScrollView>
{voucherList.map((item, index) => {
var voucher = item;
var isVoucherClaimed = isClaimedAPI(voucher.voucherID, uid);
if (
isVoucherClaimed === false
) {
return <Text>{item.name}<Text>
}
})}
</ScrollView>
);
Currently I am getting the error message "rendered more hooks than the previous rerender" and to be honest I am completely stuck with this. I have tried many different avenues to try and solve this and I can't seem to get it working - I know to use async / await / useEffect but still can't seem to get it working. If anyone has an answer it would be greatly appreciated.