Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

151
Views
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 answers
Answer question

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 Report

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 Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!