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

378
Visualizações
What is correct way to call function inside setState(useState) hooks

I want to update state at real time and call function inside setState but i am getting error.

onClick={() => setDetails(prevState => ({...prevState,id: product.orderId,status: 'processing'}) => updateStatus() )}

enter image description here

Can someone tell me correct syntax and expression

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

0

It seems you are using hooks. You can't call a function in setState like in class components. You need to watch it through useEffect.

React.useEffect(() => {
  updateStatus() 
}, [state.id, state.status])
about 4 years ago · Juan Pablo Isaza Relatório

0

the function created using useState hook doest have a second argument. it will not support call back function after updating the state. it was only there in the setState function which is used in class components.

You can use useEffect instead

useEffect(()=> {
   updateStatus()
}, [details])

   return (
     ...
    onClick={() => setDetails(prevState => ({...prevState,id: product.orderId,status: 'processing'}))}
     ...
    )

in this way, on every setDetails update it will call useEffect and then updateStatus

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