Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

100
Visualizações
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 Respostas
Responde à pergunta

0

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

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

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 Relatório

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 Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda