Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Error: Too many re-renders , when modifying an array

Getting error when modifying array through large number of iterations.

 data.logData1[0].data.map((values, index) => {
    var result = {};
    data.logData1[0].mnemonicList
      .split(",")
      .forEach((key, i) => (result[key] = values.split(",").map(Number)[i]));

    setGraphData([...graphData, result]); //Modifying Array (here comes trouble)

  });

>Solution :

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

Its difficult to say without code component, but I suspect that the problem lies in the fact that you are calling your state setter immediately inside the function component body, which forces React to re-invoke your function again, with the same props, which ends up calling the state setter again, which triggers React to call your function again…. and so on.

const resultData =  data.logData1[0].data.map((values, index) => {
    var result = {};
    data.logData1[0].mnemonicList
      .split(",")
      .forEach((key, i) => (result[key] = values.split(",").map(Number)[i]));

   return result

  });

// somewhere in your useEffect or in function

 setGraphData([...graphData, resultData]); 
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading