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

154
Visualizações
How to force a functional component to rerender on submit?

I have form inputs that update their form state(X) with an onChange then i have a form onSubmit submit that has to do 2 things:

  1. update a separate state(Y) using info from state(X)
  2. run an actual function that would use state(Y)
const [ethValue, setEthValue] = React.useState({ethValue: 0});

const submit = (e) => {
    setEthValue(() => { return { ethValue: formData.value } })
    e.preventDefault();
    runContractFunction()
};

It is working, however in order for it to complete the task I have to press Submit button twice, due to asynchronous nature of set functions. In order for it to finish setting i decided to use re-rendering so it would finish setting the state, but neither of methods i tried, work.

Since I use functional components i cannot use forceUpdate() and i cannot put useEffect inside of my submit function because it would put the useEffect hook outside of the component function body.

I want to make it so you have to press Submit once and not twice for it to work. How do I solve this problem?

Thanks in advance for your time!

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

0

Pass that 'Y' value to the function as argument which uses it as state.

const submit = (e) => {
    setEthValue(() => { return { ethValue: formData.value } })
    e.preventDefault();
    runContractFunction({
      ethValue: formData.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