How to avoid a no-param-reassign eslint error when setting a property on a dom Object with JavaScript?

Estimated read time 1 min read

The “no-param-reassign” rule in ESLint is used to prevent modification of function arguments. If you want to set a property on a DOM object and avoid the “no-param-reassign” error, you can use a destructuring assignment to create a new object with the desired changes.

Here’s an example:

let element = document.getElementById("someId");
let updatedElement = {...element, property: "newValue"};

In this example, the element object is destructured into a new object updatedElement, which has all the same properties as element, but with an updated property value. Since a new object is being created, the “no-param-reassign” error is avoided.

You May Also Like

More From Author

+ There are no comments

Add yours

Leave a Reply