Eslint-plugin-react: Some cases should not be triggered **no-unused-state**

Created on 25 Dec 2017  路  10Comments  路  Source: yannickcr/eslint-plugin-react

no-unused-state

state = {
  id: null,
  name: '',
  age: 0,
}
const {
  id,
  ...data
} = this.state;

console.log('data', data);

name and age should not be reported as error [eslint] Unused state field: 'departmentId' (react/no-unused-state).

enhancement help wanted

Most helpful comment

Note that {...this.state} also already silences the rule.

Given that, it seems that option 2 is indeed the proper direction.

All 10 comments

Using spread here offers only two options:

  1. The current situation, where you need to either explicitly use them all or use an override comment
  2. When you use spread, all state properties are marked as used, rendering the rule entirely useless, silently

Which would be more useful?

Dunno which one is actually more useful, it's a hard choice as each approach has some serious drawbacks/tradeoffs.

Just pasting in other (although similar) pattern causing "false positives" for this this rule:

class App extends React.Component {
  constructor() {
    super();

    this.state = {
      a: spring(0),
    };
  }
  render() {
    return (
      <Motion style={this.state}>
        {a => {
          count.push(a);
          return null;
        }}
      </Motion>
    );
  }
}

In that case, passing this.props or this.state anywhere is a massive antipattern; so doing it should be very inconvenient.

@ljharb "use an override comment" is nice. But I do not know this usage.

How to use JSDoc to comment id and data?

const {
  id,
  ...data
} = this.state;

JSDoc has nothing to do with anything.

In this case, you'd add // eslint-disable-line react/no-unused-state on the line that was warning.

@ljharb This is not elegant, but only so. Thanks.

In the case of spread, I would argue that (2) is more useful. In such cases, a typechecker with an exact object type should be used to prevent extraneous properties.

Note that {...this.state} also already silences the rule.

Note that {...this.state} also already silences the rule.

Given that, it seems that option 2 is indeed the proper direction.

I have to agree with option #2 in the least. My problem is I am rendering a component from a function:

{getComponent(this.state)}

And the rule goes haywire at that point. I guess I could make getComponent a class method and then the rule works, but that makes my code less clean (React is supposed to be functional-friendly).

I may need to turn off this rule, which is unfortunate, because most of the time, it is a big help and has caught some needless state.

I鈥檇 suggest making it an SFC instead and using jsx rather than a function call (renderFoo class methods are an antipattern).

Was this page helpful?
0 / 5 - 0 ratings

Related issues

fastfedora picture fastfedora  路  3Comments

otakustay picture otakustay  路  3Comments

Arcanemagus picture Arcanemagus  路  3Comments

inian picture inian  路  3Comments

CarlRosell picture CarlRosell  路  3Comments