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

For loop in useEffect too many re-renders

I am working on a for loop that starts with 1 column at the window.innerHeight of 530px and increments with +1 if the window.innerHeight gets incremented with 40.

This is what I have right now, but it says too many re-renders everytime.
Does anyone know why?

let gridcolumns = 1;
  useEffect(() => {
    for (let i = 530; i < window.innerHeight; i += 40) {
      gridcolumns += 1;
    }
  });
  setColumnsAmount(gridcolumns);

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

>Solution :

You need to add dependency list to your useEffect :

let gridcolumns = 1;
  useEffect(() => {
    for (let i = 530; i < window.innerHeight; i += 40) {
      gridcolumns += 1;
    }
  setColumnsAmount(gridcolumns);
  },[]);
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