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

165
Visualizações
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 Respostas
Responde à pergunta

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 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