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

143
Visualizações
Improving Caclculator inside react progrem

I'm trying to add a reset button to the input (Kind of like CE in a calculator) and i'm having trouble with placing it inside the code.

import { useRef } from "react";
import './App.css';

function App() {
  const a = useRef(0);
  const b = useRef(0);

  const plusaction = () => {
    console.log(a.current.value);
    console.log(b.current.value);
    alert(parseInt(a.current.value) + parseInt(b.current.value));
  };

  const Minusaction = () => {
    console.log(a.current.value);
    console.log(b.current.value);
    alert(parseInt(a.current.value) - parseInt(b.current.value));
  };

  const MultiplyAction = () => {
    console.log(a.current.value);
    console.log(b.current.value);
    alert(parseInt(a.current.value) * parseInt(b.current.value));
  };
  const DivideAction = () => {
    console.log(a.current.value);
    console.log(b.current.value);
    alert(parseInt(a.current.value) / parseInt(b.current.value));
  };

  return (
    <div className="App">
      <header className="App-header">
      <h1> Enter the numbers Down</h1>
      <div>
      <input type="number" ref={a} placeholder="0" />
      <input type="number" ref={b} placeholder="0" />

      </div>
      <div className="margin"><p></p></div>
      <div>
      <button onClick={plusaction}>+</button>
      <button onClick={Minusaction}>-</button>
      <button onClick={MultiplyAction}>*</button>
      <button onClick={DivideAction}>/</button>
      </div>
      </header>
    </div>
  );
}

export default App;

Any suggestions for improvment and of course add the CE button? Thank you in advance.

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

0

You need to learn how to use useState, using ref in this way is not correct in a react program.

Once you have state clearing the state will be very trivial. Look at any example of useState with textfields

about 4 years ago · Juan Pablo Isaza Relatório

0

use react useState hook.

When you change a value using the setState function, the value changes automatically where the state is used.

import { useState } from 'react';
import './App.css';

function App() {
  const [valueA, setValueA] = useState(0);
  const [valueB, setValueB] = useState(0);
  const [sum, setSum] = useState(0);

  const handleResult = e => {
    const operator = e.target.textContent;
    switch (operator) {
      case '+':
        setSum(valueA + valueB);
        return;
      case '-':
        setSum(valueA - valueB);
        return;
      case '*':
        setSum(valueA * valueB);
        return;
      case '/':
        setSum(Math.floor(valueA / valueB));
        return;
      default:
        return;
    }
  };

  const handleValueA = e => {
    const value = e.target.value;
    if (!value) return;
    setValueA(parseInt(value, 10));
  };

  const handleValueB = e => {
    const value = e.target.value;
    if (!value) return;
    setValueB(parseInt(value, 10));
  };

  const handleReset = () => {
    setValueA(0);
    setValueB(0);
    setSum(0);
  };

  return (
    <div className="App">
      <header className="App-header">
        <h1> Enter the numbers Down</h1>
        <div>
          <input
            type="number"
            placeholder="0"
            onChange={handleValueA}
            value={valueA}
          />
          <input
            type="number"
            placeholder="0"
            onChange={handleValueB}
            value={valueB}
          />
          <div className="result">{sum}</div>
        </div>
        <div className="margin">
          <p></p>
        </div>
        <div>
          <button onClick={handleResult}>+</button>
          <button onClick={handleResult}>-</button>
          <button onClick={handleResult}>*</button>
          <button onClick={handleResult}>/</button>
          <button onClick={handleReset}>reset</button>
        </div>
      </header>
    </div>
  );
}

export default App;
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