I am trying to set a variable inside an onClick event. I am setting the error messages into the variable.
const handleEdit = (e) => {
e.preventDefault();
if (edit_user.email == "") {
setEditUserError({ email: "Please enter an email!" })
}
if (edit_user.company == "") {
setEditUserError({ ...edit_user_error, company: "Please enter company!" })
}
And I am showing the error in the form like <span className="text-danger">{edit_user_error.company}</span>
The problem is that when all the fields are empty and when I click the button only the last message is shown. When I make one field empty, and then click the submit button it is working correctly. I tried console.log(edit_user_error) but it is showing empty. What am I doing wrong? Please help