Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

164
Vistas
Why is <input type='number' value={var_name}> replacing all the values in a table?

I am trying to map out a number of elements in a table and each of the elements has a specific value. Whenever i use value={inputArrival} or value={inputBurst} all the values of the specific column get update. I am attaching a screenshot. How to fix this? Screenshot of what's happening

import React,{useState} from 'react';
import './tableEdit.css';
const EntryTable = (props) => {
    const entry=props.numOfEntries;
    const[inputArrival,SetinputArrival]=useState('');
    const[inputBurst,SetinputBurst]=useState('');
 
    function changeArrival(e)
    {
       
           
        console.log(e.target.value+"A");
        /*
        if(!Number(e.target.value))
       return;
        else*/
        SetinputArrival(e.target.value);
    }
    function changeBurst(e)
    {
        console.log(e.target.value+"B"); 
        SetinputBurst(e.target.value);
     
    }

   
   // console.log(entry);
    var ArrayEntry=[];
    for(var i=1;i<=entry;i++)
    ArrayEntry.push( <tr key={i}>                       
        <td className='row_editContent' >P{i}</td>
        <td className='row_editContent'><input className='input_edit' placeholder='0' type='number' value={inputArrival} onChange={(e)=>changeArrival(e)}></input>ms</td>
        <td className='row_editContent'><input  className='input_edit' placeholder='0' type='number' value={inputBurst} onChange={(e)=>changeBurst(e)}></input>ms</td>
        </tr>);

        return (
        <div>
            <table className='MainTableContainer' >
                <thead>
                    <tr >
                        <th className='row_editHeading'>Process</th>
                        <th className='row_editHeading'>Arrival Time</th>
                        <th className='row_editHeading'>Burst Time</th>
                    </tr>
                </thead>
                <tbody> 
                    {ArrayEntry}
                </tbody>
            </table>
        </div>
    )
}

export default EntryTable

about 4 years ago · Juan Pablo Isaza
1 Respuestas
Responde la pregunta

0

You are using the same single states inputArrival and inputBurst for every row entry of data in the table. Update so each is an array of strings, and update the changeArrival and changeBurst callbacks to curry an index to update. Use the mapped index to access the correct state and pass to the onChange callbacks.

const EntryTable = (props) => {
  const entry = props.numOfEntries;
  const [inputArrival, setInputArrival] = useState(Array(entry).fill(""));
  const [inputBurst, setInputBurst] = useState(Array(entry).fill(""));

  function changeArrival(index) {
    return (e) => {
      console.log(e.target.value + "A");
      setInputArrival((values) =>
        values.map((value, i) => (i === index ? e.target.value : value))
      );
    };
  }
  function changeBurst(index) {
    return (e) => {
      console.log(e.target.value + "B");
      setInputBurst((values) =>
        values.map((value, i) => (i === index ? e.target.value : value))
      );
    };
  }

  // console.log(entry);
  const ArrayEntry = Array.from({ length: entry}).map((_, i) => (
    <tr key={i}>
        <td className="row_editContent">P{i}</td>
        <td className="row_editContent">
          <input
            className="input_edit"
            placeholder="0"
            type="number"
            value={inputArrival[i]}
            onChange={changeArrival(i)}
          />
          ms
        </td>
        <td className="row_editContent">
          <input
            className="input_edit"
            placeholder="0"
            type="number"
            value={inputBurst[i]}
            onChange={changeBurst(i)}
          />
          ms
        </td>
      </tr>
  ));

  return (
    <div>
      <table className="MainTableContainer">
        <thead>
          <tr>
            <th className="row_editHeading">Process</th>
            <th className="row_editHeading">Arrival Time</th>
            <th className="row_editHeading">Burst Time</th>
          </tr>
        </thead>
        <tbody>{ArrayEntry}</tbody>
      </table>
    </div>
  );
};

Edit why-is-input-type-number-value-var-name-replacing-all-the-values-in-a-tabl

about 4 years ago · Juan Pablo Isaza Denunciar
Responde la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda