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

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

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

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