Skip to content

How to avoid "Assignment to property of function parameter 'elem'" #1653

Closed
@darde

Description

@darde

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

ljharb commented on Dec 6, 2017

@ljharb
Collaborator

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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

      Development

      No branches or pull requests

        Participants

        @ljharb@darde

        Issue actions

          How to avoid "Assignment to property of function parameter 'elem'" · Issue #1653 · airbnb/javascript