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

200
Vistas
the handleSubmit function doesn't work in if sttaement

this is my code everything is ok but if statement doesn't work if i erase the if part of code the request works just fine but when it's inside of if it does't work my errors object is empty

 const handleSubmit=(e)=> {
    e.preventDefault();
    setErrors(validate(values))

      if (!errors) {
          console.log(errors);
          baseUrl.get("/sanctum/csrf-cookie").then((response) => {
            baseUrl
              .post("/api/v1/admin-login", values)
              .then((response) => {
                console.log(response);
              })
              .catch((err) => {});
          });
     }
        

and the errors state

  const [errors, setErrors] = useState({});
about 4 years ago · Juan Pablo Isaza
2 Respuestas
Responde la pregunta

0

that's because the value of error is not changed yet.

you can fix it in this way


const [errors, setErrors] = useState({})

const handleSubmit=(e)=> {
    e.preventDefault();
    const errors = validate(values) 
    setErrors(errors)

      if (!errors) {
          console.log(errors);
          baseUrl.get("/sanctum/csrf-cookie").then((response) => {
            baseUrl
              .post("/api/v1/admin-login", values)
              .then((response) => {
                console.log(response);
              })
              .catch((err) => {});
          });
     }
about 4 years ago · Juan Pablo Isaza Denunciar

0

The value has not changed yet because you set errors for the update state in the current function and the next line you want to use. You can use the local variable in the function to check errors.

setState() does not always immediately update the component. It may batch or defer the update until later. This makes reading this. state right after calling setState() a potential pitfall. https://reactjs.org/docs/react-component.html#setstate

const handleSubmit=(e)=> {
e.preventDefault();
const errors = validate(values) 
setErrors(errors)
  if (!errors) {
      console.log(errors);
      baseUrl.get("/sanctum/csrf-cookie").then((response) => {
        baseUrl
          .post("/api/v1/admin-login", values)
          .then((response) => {
            console.log(response);
          })
          .catch((err) => {});
      });
 }
about 4 years ago · Juan Pablo Isaza Denunciar
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