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

80
Views
Mi aplicación de reacción no calcula correctamente (solo después de que el segundo símbolo muestre el resultado)

Estoy trabajando en una aplicación de reacción. Tiene un botón Agregar para agregar línea para calcular el volumen. Cada línea contiene 2 campos de entrada para valores de espesor y área de superficie correspondiente. Noté que si se ingresó el grosor y el usuario comienza a ingresar el área de la superficie, el volumen se representará solo después del segundo símbolo.

Por ejemplo, espesor - ingresado 100, superficie - 1 y volumen 0 (pero esperado 0,1). Luego superficie - 10 y volumen 1 (como se esperaba). Lo mismo si entrada superficie 100 y espesor 1 --> volumen 0 (esperado 0,1).

¿Alguien podría explicar cómo solucionar el problema, por favor? Gracias por tu ayuda.

codigosandbox

 import { useState } from "react"; const App = () => { const [datas, setDatas] = useState([]); const handleChange = (index) => (event) => { let currentValue = +event.target.value; if (event.target.name === "thickness") { let newArr = [...datas]; newArr[index] = { ...newArr[index], thickness: currentValue }; if (datas[index].thickness && datas[index].surface) { newArr[index] = { ...newArr[index], volume: (currentValue * newArr[index].surface) / 1000, }; } setDatas(newArr); } if (event.target.name === "surface") { let newArr = [...datas]; newArr[index] = { ...newArr[index], surface: currentValue }; if (datas[index].thickness && datas[index].surface) { newArr[index] = { ...newArr[index], volume: (newArr[index].thickness * currentValue) / 1000, }; } setDatas(newArr); } }; const removeItem = (index) => { let newItems = datas.filter((product, id) => id !== index); setDatas(newItems); }; const addLine = () => { setDatas([ ...datas, { thickness: 0, surface: 0, volume: 0, }, ]); }; return ( <> {datas.map((data, index) => { return ( <li key={index}> <input label="thickness mm" name="thickness" id={index} value={data.thickness} onChange={handleChange(index)} /> <input name="surface" label="surface m¬2" id={index} value={data.surface} onChange={handleChange(index)} /> <span>Volume {data.volume}</span> <button onClick={() => removeItem(index)}>X</button> </li> ); })} <button onClick={addLine}>Add</button> </> ); }; export default App;
about 4 years ago · Juan Pablo Isaza
1 answers
Answer question

0

Tiene un problema con sus condiciones, está comprobando si los valores antiguos están completos en lugar de comprobar las variables newArr or currentValue .

Este es un ejemplo: si le damos al thickness el valor 100 cuando el usuario comienza a ingresar la input surface , verifica si la variable datos [índice]. superficie (valor anterior) está llena en lugar de verificar el valor actual.

 const handleChange = (index) => (event) => { let currentValue = +event.target.value; if (event.target.name === "thickness") { let newArr = [...datas]; newArr[index] = { ...newArr[index], thickness: currentValue }; if (newArr[index].thickness && newArr[index].surface) { //you should verify the latest values not old newArr[index] = { ...newArr[index], volume: (currentValue * newArr[index].surface) / 1000 }; } setDatas(newArr); } if (event.target.name === "surface") { let newArr = [...datas]; newArr[index] = { ...newArr[index], surface: currentValue }; if (newArr[index].thickness && newArr[index].surface) { //you should verify the latest values not old newArr[index] = { ...newArr[index], volume: (newArr[index].thickness * currentValue) / 1000 }; } setDatas(newArr); } };

https://codesandbox.io/s/intelligent-liskov-dghgu?file=/src/App.js:99-939

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!