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

159
Vistas
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 Respuestas
Responde la pregunta

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 Denunciar

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 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