Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

107
Views
¿Cómo puedo obtener un valor antiguo en el evento onChange de entrada?

Tengo algunas entradas definidas en reaccionar. Estoy asignando una función a su función onChange . Puedo encontrar el valor actual. ¿Cómo puedo obtener el valor anterior de la manera más simple?

La razón por la que quiero esto es porque quiero encontrar la relación entre este nuevo valor y el valor anterior y hacer algunas operaciones.

Código de muestra:

 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 answers
Answer question

0

Si el valor anterior no es necesario en el estado del componente, use una referencia como la siguiente, puede cambiar su valor inicial

 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 Report

0

Puedes usar un estado para hacerlo:

 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 Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!