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

101
Vistas
setState doesnot change again once its set

I am using Axios to post data to the server everything is working just fine but I wanted to show error message if email is already exists using a state hook but state does not change once its set can anyone help me what i am doing wrong

const [serverError, setServerError] = useState(false);
  const handleRegister = async ({ name, email, password }) => {
    const userdata = { name, email, password };
    const res = await apiUrl.post("/users", userdata);
    if (!res.statusText === "OK") return setServerError(true);
    setServerError(false);
    console.log(serverError);
    console.log(res.data);
    localStorage.setItem("token", res.headers["x-auth-token"]);
  };
about 4 years ago · Juan Pablo Isaza
3 Respuestas
Responde la pregunta

0

You need to use != operator to check for inequality.

if (res.statusText != "OK") return setServerError(true);
about 4 years ago · Juan Pablo Isaza Denunciar

0

The issue is here:

if (!res.statusText === "OK")

You have to use !== or != ( first one is equality check with datatype and second one is equality check. So the condition will be like:

if (res.statusText != "OK")
{
  return setServerError(true);
}
about 4 years ago · Juan Pablo Isaza Denunciar

0

I think you have error here

if (!res.statusText === "OK")

As you have defined a NOT operator before the variable it treats the variable to Boolean value that means Boolean === String => false always

Just keep

if (res.statusText === "OK")

And in case you want to check if there is value or not use in response.

if (res?.statusText === "OK")
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