Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

318
Vistas
Email is not sent even if the error validation object is empty on React Js

I'm trying to send an email when the object error is empty but is not working, even if it is null and doesn't contain any error.

I've checked the validation with an console log, but is giving up the errors that are missing, but I don't know where is the cach.

Here is the contact form function:

import React, { useState, useRef } from 'react'
import './ContactForm.scss'
import emailjs from "emailjs-com"
import '../../shared/../../components/pages/Form/shared/LoginRegister.scss'
import * as styles from '../Form/Globals.module.scss'
import ContactValidation from '../../../utils/ContactValidation'

export default function ContactForm() {

    const [values, setValues] = useState({ name: "", email: "", phone: "", subject: "", description: "" });
    const [errors, setErrors] = useState({});

    const form = useRef();

    const ContactHandler = (e) => {
        e.preventDefault();
        setErrors(ContactValidation(values));

        if (errors === 0 || errors === {} || errors === null || errors === []) {
            emailjs.sendForm('api_hidden', 'swiftquiz_template', form.current, 'api_hidden')
                .then((result) => {
                    console.log(result.text);
                    alert("Sent Successfully ^_^");
                }, (error) => {
                    console.log(error.text);
                });

            setValues({ name: "", email: "", phone: "", subject: "", description: "" });
        }

        else {
            console.log("All Fields are required");

        }

    }

    return (
        <>
           {i_removed_components_for_simpicity}
        </>
    )
}

Here is the validation:

export default function ContactValidation(values){

    let errors = {};

    if(!values.name.trim()){
        errors.name = "Name is required."
    }
    if(!values.email){
        errors.email = "Email is required."
    }else if(!/\S+@\S+\.\S+/.test(values.email)){
        errors.email = "Email is invalid.";
    }
    
    if(!values.subject.trim()){
        errors.subject = "Subject is required."
    }
    if(!values.description.trim()){
        errors.description = "Description is required."
    }
    return errors;

}
about 4 years ago · Juan Pablo Isaza
Responde la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda