Closed
Description
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
Activity
ljharb commentedon Dec 6, 2017
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.