0

I've got a problem with sending emails in React by EmailJs. When i validate form and all the errors desapears, form is sending email only after second click and i dont really know why this is happening why. Please help

const useForm = (callback, validate) => {
  const [values, setValues] = useState({
    title: "",
    email: "",
    message: "",
  });
  const [errors, setErrors] = useState({});
  const [send, setSend] = useState(false);
  const [isSubmiting, setIsSubmiting] = useState(false);

  useEffect(() => {
    if (Object.keys(errors).length === 0) {
      if (isSubmiting) {
        setSend(true);
      }
    }
  }, [errors]);

  const handleChange = (e) => {
    const { name, value } = e.target;
    setValues({
      ...values,
      [name]: value,
    });
  };

  const handleSubmit = (e) => {
    e.preventDefault();
    setErrors(validate(values));
    setIsSubmiting(true);
    if (send) {
      emailjs
        .sendForm(
          "service",
          "templatekey",
          e.target,
          "userkey"
        )
        .then(
          (result) => {
            console.log(result.text);
          },
          (error) => {
            console.log(error.text);
          }
        );
      e.target.reset();
    }
  };

  return { handleChange, values, handleSubmit, errors };
};

export default useForm;

1
  • share the errors and just the part of code that is interesting or where you think the error is. Commented Dec 13, 2020 at 10:03

2 Answers 2

1

After moving setErrors(validate(values)) and setIsSubmiting(true) to handleChange it works fine for me :)

const handleChange = (e) => {
const { name, value } = e.target;
setValues({
  ...values,
  [name]: value,
});
setErrors(validate(values));
setIsSubmiting(true);

};

const handleSubmit = (e) => {
e.preventDefault();
if (send) {
  console.log("WYSYŁAM");
  emailjs
    .sendForm(
      "service",
      "template",
      e.target,
      "user"
    )
    .then(
      (result) => {
        console.log(result.text);
      },
      (error) => {
        console.log(error.text);
      }
    );
  e.target.reset();
}

};

return { handleChange, values, handleSubmit, errors }; };

Sign up to request clarification or add additional context in comments.

Comments

0

I ran into the same problem and found that adding an 'onClick' event handler to your form button will prevent the user from needing to double click.

<button onClick={handleClick}> Submit </button>
                

in useForm.js I moved setErrors(validate(values)); and setIsSubmitting(true); into handleClick

function handleClick() {
    setErrors(validate(values));
    setIsSubmitting(true);
};

const handleSubmit = e => {
    e.preventDefault();

    if (send) {
        emailjs.sendForm('service', 'templateKey', e.target, 'userKey')
            .then((result) => {
                console.log(result.text);
            }, (error) => {
                console.log(error.text);
            });
        callback();
    }
};

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.