Eslint-plugin-react: "no-unused-state" w/ Used State as prevState

Created on 20 Mar 2019  路  3Comments  路  Source: yannickcr/eslint-plugin-react

Version: 7.12.4

Code:

import React from 'react';

class ComponentX extends React.Component {
  constructor(props) {
    super(props);

    this.state = {
      counter: 0,
      // Unused state field: 'allowUpdates'eslint(react/no-unused-state)
      allowUpdates: true
    };
  }

  update() {
    this.setState(prevState => {
      if (prevState.allowUpdates) {
        // (... but the state is used here?)
        const nextCounter = prevState.counter + 1;

        return {
          counter: nextCounter
        };
      }
    });
  }

  render() {
    return (
      <div>
        <div>Counter = {this.state.counter}</div>{" "}
        <button onClick={() => this.update()}>Update</button>
      </div>
    );
  }
}

export default ComponentX;

CodeSandbox: https://codesandbox.io/s/2o9pvqnyky

Screengrab:

Screen Shot 2019-03-20 at 3 27 41 PM

bug help wanted

Most helpful comment

Yes, unfortunately, it still complains when destructuring like that.

All 3 comments

If you instead do this, does it still complain?

update() {
    this.setState(({ counter, allowUpdates }) => {
      if (allowUpdates) {
        // (... but the state is used here?)
        const nextCounter = counter + 1;

        return {
          counter: nextCounter
        };
      }
    });
  }

Yes, unfortunately, it still complains when destructuring like that.

This seems to be working fine with versions:

"eslint": "^7.3.1",
"eslint-plugin-react": "^7.20.2"

As the picture shows, hello is not being used and triggers the rule but allowUpdates is only being used with prevState as in the issue description.

Screen Shot 2020-07-08 at 3 21 23 PM

Was this page helpful?
0 / 5 - 0 ratings