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

108
Visualizações
How can I get old value in input's onChange event?

I have some inputs defined in react. I'm assigning a function to their onChange function. I can find the current value. How can I get the old value in simplest way?

The reason I want this is because I want to find the ratio of this new value to the old value and do some operations.

Sample code:

let onChange = (e) => {
    let newValue = e.target.value; // let oldValue = will be here     

}

<Input type="number"  onChange={onChange }></Input>
about 4 years ago · Juan Pablo Isaza
2 Respostas
Responde à pergunta

0

If the old value is not needed in the component state, use a ref like the following, you can change its initial value

const oldValueRef = React.useRef(0)

let onChange = (e) => {
    const oldValue = oldValueRef.current;
    let newValue = e.target.value; // let oldValue = will be here

    // after everything     
    oldValueRef.current = e.target.value;
}

<Input type="number" onChange={onChange }></Input>
about 4 years ago · Juan Pablo Isaza Relatório

0

You can use a state to do it:

const [state, setState] = useState("");

let onChange = (e) => {
  let oldValue = state; 
  let newValue = e.target.value;
  setState(newValue)
  // your logic
};

<Input type="number" value={state} onChange={onChange}></Input>;
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