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

110
Visualizações
Simple Calculator Using React

I am trying to build a very simple calculator that just takes the input from two text boxes, and adds the values together when you press the Add button and subtracts the two values when you press the subtract button. The total should output to a third text box. This is what I have so far:

import React, { useState } from 'react';
import './App.css';
class Calculator extends React.Component{
  constructor(){
    super();
    this.state={
      number1:"",
      number2:"",
      total: ""
    }
  }
handlenumber1 = (event) => {
  this.setState({number1:event.target.value})
}
handlenumber2 = (event) => {
  this.setState({number2:event.target.value})
}


exe = (e) => {
  e.preventDefault();
  this.setState({total: parseInt(this.state.number1) + parseInt(this.state.number2)})
}


  render(){
    return( 
    <div>
      <h1>Simple Calculator</h1>
      <form onSubmit={this.exe}>
      <div>
      Number 1: 
      <input type="text" value={this.state.number1} onChange={this.handlenumber1}/>
      </div>
      <div>
      Number 2: 
      <input type="text" value={this.state.number2} onChange={this.handlenumber2}/>
      </div>
      <div>
      <button type= "submit"> Add </button>
      </div>
      <div>
      <button type= "submit"> Subtract </button>
      </div>
      </form>
      <input type = "text" name = "name" value = {this.state.total}/>
      </div>
    )
  }

}

export default Calculator;

As of now, I am able to get the addition output to the textbox but I am confused on getting the subtraction output to the same textbox using a different button. I assume I would use something like this.setState({total: parseInt(this.state.number1) - parseInt(this.state.number2) but I am unsure of how to get it into the same textbox. Thanks in advance!

about 4 years ago · Juan Pablo Isaza
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