0

In a table, certain input text fields are displayed. Accordingly data could be inputted in it. My intention is to club all the input data into an array. Each record has its specific unique id, which we get in console while we input in text box. Based on this id, I want to club into an array data. I've tried with one logic but gives error. Please have a look at the code below

// Here newData is an array of records which I'm displaying in Grid
const [dataNew, setDataNew] = useState(newData);

const textChange = (data) => {
    const { id, value } = data;
    setDataNew((prevInfo) => {
      const dataIndex = +id[id.length - 1];
      return {
        ...prevInfo,
         
        // Here I'm getting error in the below code snippet in prevInfo
        dataHere: Object.assign([...prevInfo.newData], { [dataIndex]: value })
      };
    });
  };

console.log('NEW DATA', newData)

Please suggest me if any changes to be done. Any solution highly appreciated

Please refer codesandbox link --> https://codesandbox.io/s/elated-varahamihira-xpjtdb?file=/src/Table.js:149-197

1 Answer 1

1

dataNew is initially an array, but you are returning an object from the setDataNew callback.

Also id is a number itself in your sandbox, so the following would suffice:

const textChange = (data) => {
  const { id, value } = data;
  setDataNew((prevInfo) => {
    const dataIndex = id - 1;
    prevInfo[dataIndex] = value;
    return [...prevInfo];
  });
};
Sign up to request clarification or add additional context in comments.

2 Comments

Perfect answer, as expected. Thanks
One issue I had... what if id is changed... as I've done it and getting undefined... can you please help in this stackoverflow.com/questions/73764649/…

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.