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

149
Visualizações
React : can't update my state with setState

i try to insert some data in my state, but when i try, i've got an error on user_email: "test@mail.com" : The type cast expression is expected to be wrapped with parenthesis

But i don't understand where am i supposed to put parenthesis. This is the code :

const [userLogin, setUserLogin] = useState({
    user_email: "",
    user_password: "",
  });


  const login = async (e) => {
    e.preventDefault();
    console.log(refConnexionMail.current.value); 
    setUserLogin(
      ...userLogin,
      user_email: 'test@mail.com'
    )
    await POST(ENDPOINTS.USER_LOGIN, userLogin);
    // await GET(ENDPOINTS.USER_LOGIN)
    fetch("http://localhost:4200/api/auth/login")
      .then((response) => response.json())
      .then((data) => {
        console.log(data);
      });
  }; 

and this is the error :enter image description here

Then, i will replace "test@mail.com" by "refConnexionMail.current.value" which is the data i want to put in my state.

about 4 years ago · Juan Pablo Isaza
2 Respostas
Responde à pergunta

0

In your code, you are using the spread operator You have to put the parenthesis here

setUserLogin({
      ...userLogin,
      user_email: 'test@mail.com'
    }) 
about 4 years ago · Juan Pablo Isaza Relatório

0

The type cast expression is expected to be wrapped with parenthesis.

It has to have parenthesis here:

setUserLogin({
   ...userLogin,
   user_email: 'test@mail.com'
})

setState being an async operation, you may or may not have the right value for your POST method. So better do something like this.

setUserLogin((prevState) => {
     return {
        ...prevState,
        user_email: 'test@mail.com'
     }
})
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