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

warning : function declared in a loop contains unsafe references to variable(s) ''

for (let i = 0; i < state.columns.length; i++) {
  var column = state.columns[i];
  var sortIndex;
  var fsColumn = firstSortColumns.find((v, i) => {
    sortIndex = i;
    return v.dataField === column.dataField;
  });
  if (fsColumn) {
    //...
  } else {
    //...
  }
}

Function declared in a loop contains unsafe references to variable(s) ‘sortIndex’, ‘column’.

I am getting a warning like this, how can i fix this?

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 :

Your linter is being overly cautious here. Since the function you pass to find isn’t called asynchronously, the fact the value of column can change isn’t a problem.

If it were async then you could solve the problem by using let instead of var since it has block-scope instead of function-scope (and doing so would shut up your linter).

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