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

112
Visualizações
display result in a div after submitting in React

i have a calculation function, whenever i put the the numbers in input field i want it to calculate the result when the submit button is clicked and i also want it to show the result in a div. now whenever i hit the button it displays the correct result in console, but i want to know how can i display the final result in a div inside a form with hooks?

the button :

  const handleSubmit = (e) => {
  e.preventDefault();
  console.log(solution)
  }

the form :

     <div onChange={(e) => setResult(solution)}>
      result : {setResult}
     </div>
about 4 years ago · Juan Pablo Isaza
2 Respostas
Responde à pergunta

0

I assume you have specified your state like this:

const [result, setResult] = useState();

In this case you need to change your handler to update state whenever you get a result:

const handleSubmit = (e) => {
  e.preventDefault();
  //...
  setResult(solution)
  }

and render it in the div:

<div>
    result: {result}
</div>

NOTE: div element doens't have onChange event

about 4 years ago · Juan Pablo Isaza Relatório

0

<div> doesn't have an onChange. I think what you're trying to do is listen for changes in the solution. The best way to do that is with useState. If you're keeping your solution in state with something like const [result, setResult] = useState(), the div will look more like:

<div>
  result : {result}
</div>

You just need to make sure you're updating the state correctly when the button is pressed. Maybe like:

const handleSubmit = (e) => {
  e.preventDefault();
  //... do calculation and assign it to solution variable
  setResult(solution)
  }
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