Javascript: How to avoid "Assignment to property of function parameter 'elem'"

Created on 6 Dec 2017  路  1Comment  路  Source: airbnb/javascript

Hi guys, I want to understand how you guys avoid the eslint warning "Assignment to property of function parameter 'elem'". I know that is a good pattern do not change function parameters values. This keeps our code decoupled and with high maintenance. However, in some cases, I come across with the following situation.
I need to retrieve all elements with a specific class, and then, change its display style property.
This is my code:

const setDisplayStyleToElementsArray = (arr, display) => {
  arr.map((elem) => {
    elem.style.display = display;
    return elem;
  });
};

const elements = document.getElementsByClassName('.myClass');

const myFields = [].slice.call(elements, 0);

setDisplayStyleToElementsArray(myFields, 'block');

In this case, I'm changing the property "style" of all my .myClass elements. What is the correct way of doing that? How would airbnb development team handle such case?

Thanks in advance

Pablo Darde
front end engineer

question

Most helpful comment

We avoid it because we use React, and thus we never work with DOM nodes directly.

In this case, I'd suggest using an eslint override comment for the places you find you need to do that.

>All comments

We avoid it because we use React, and thus we never work with DOM nodes directly.

In this case, I'd suggest using an eslint override comment for the places you find you need to do that.

Was this page helpful?
0 / 5 - 0 ratings

Related issues

stephenkingsley picture stephenkingsley  路  3Comments

brendanvinson picture brendanvinson  路  4Comments

ryankask picture ryankask  路  3Comments

tpiros picture tpiros  路  3Comments

progre picture progre  路  3Comments