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

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

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