I have a variable initialized in the following way:
let [count_farmers, set_count_farmers] = useState(0);
What I'm trying to do is, when a person is Registering to my page, I wish to increment the variable, like so:
let handleAddUser = (userinfo) => {
console.log("before: " + count_farmers);
set_count_farmers(count_farmers + 1);
console.log("after: " + count_farmers);
//check if email exists @ anyone of the users, if so, don't create!
let found_user = usersVal.find((user) => userinfo.email == user.email);
if (found_user === undefined && userinfo.email.includes("@")) {
console.log(getFarmerImageByName(count_farmers % 5));
usersEditVal([
...usersVal,
{
firstname: userinfo.firstname,
lastname: userinfo.lastname,
fullname: userinfo.firstname + " " + userinfo.lastname,
reviews: [{ idReviewer: "", text: "" }],
location: "Tel Aviv",
likes: 0,
about:
"this is a default about textthis is a default about textthis is a default about textthis is a default aboefbout text",
likedProfilesId: [],
likedProductsId: [],
img: getFarmerImageByName(count_farmers % 5),
id: v4(),
isFarmer: userinfo.isFarmer,
password: userinfo.password,
email: userinfo.email,
farmerProducts: [],
cartProducts: [],
soldItems: [],
},
]);
return true;
} else {
console.log("EMAIL IS INVALID.");
return false;
}
//****************************************************************** */
};
Now, doesn't matter how many times I register, the console log of before and after (1st and 3rd lines in the function handleAddUser), I get 0 printed to the screen.
Any ideas?
Regards!