ali
ali

Reputation: 151

'Error' message: ''index' is defined but never used. (no-unused-vars)

've setup eslint & eslint-plugin-react.

When I run ESLint, the linter returns no-unused-vars errors for each React component.

I'm assuming it's not recognizing that I'm using JSX or React syntax. Any ideas? enter image description here

this my code--

Upvotes: 1

Views: 6876

Answers (3)

Estus Flask
Estus Flask

Reputation: 222369

As another answer already explains, no-unused-vars rule triggers linter error because index parameter isn't in use. It can be omitted:

users.forEach(user => {
  /* ... */
});

If for some reason a parameter is temporarily not in use but is expected to be used later, or it's needed for proper function arity (this is not the case for forEach), it can be marked as unused (conventionally, underscored parameters treated as such):

users.forEach((user, _index) => {
  /* ... */
});

Expected parentheses around arrow function argument linter error means that it was configured to enforce optional parentheses in arrow functions with arrow-parens rule. This can be changed by either disabling this rule or adding parentheses:

users.forEach((user) => {
  /* ... */
});

The latter option may be preferred because enforced arrow parentheses are more consistent.

Upvotes: 2

connexo
connexo

Reputation: 56744

In your codeblock

users.forEach((user, index) => { ... }) 
                     ^^^^^

you never use the parameter index, rendering it useless. That why ESLint complains - rightfully.

Instead go

users.forEach((user) => { ... }) 

Upvotes: 4

ali
ali

Reputation: 151

 handleSelectAll = () => {
    const { users } = this.props.users
    this.setState((prevState) => {
      const newState = { ...prevState }
      newState.selects = []
      users.forEach(user => {
        newState.selects.push(!prevState.selectedAll)
      });
      newState.selectedAll = !prevState.selectedAll
      return newState
    })
  }

"message: 'Expected parentheses around arrow function argument. (arrow-parens)"

Upvotes: 0

Related Questions