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

224
Views
Declaration or statement expected when I try to declarate a previus state

I'm reading the react documentation, and I realized that I'm typing the syntax wrong when trying to change the state in react

So I am trying to change this to the correct form but it gives me an error

Old version

function handleInputChange() {
  setState({...state, [e.target.name]: e.target.value,
  });
}

New version

function handleInputChange() {
  setState((prev_state)=>{...prev_state,[e.target.name]: e.target.value});
}

Declaration or statement expected.

about 4 years ago · Juan Pablo Isaza
2 answers
Answer question

0

in order to return an object using the arrow function syntax you just have to wrap it with additional rounded brackets :) so you code should look like:

function handleInputChange(e) {
  setState((prev_state)=>({...prev_state,[e.target.name]: e.target.value}));
}

or of course with the old syntax it can look like:

function handleInputChange(e) {
  setState(function(prev_state){
    return {...prev_state,[e.target.name]: e.target.value}
  }
);
}
about 4 years ago · Juan Pablo Isaza Report

0

You forgot to include 'e' as the parameter

function handleInputChange(e) {
   setState((prev_state)=>{...prev_state,[e.target.name]: e.target.value});
}
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!