Skip to content

no-unused-state + arrow function property + destructuring #1363

Closed
@daltones

Description

@daltones

I found a case where no-unused-state raises a false-positive:

import React from 'react';
import PropTypes from 'prop-types';

export default class MyComponent extends React.PureComponent {
  static propTypes = {
    prop1: PropTypes.string,
  };

  state = {
    state1: false,
  };

  handleClick = () => {
    const {
      props: {prop1},
      state: {state1},
    } = this;

    console.log(prop1);
    console.log(state1);
  };

  render() {
    return <button onClick={this.handleClick} />;
  }
}

Warns: Unused state field: 'state1' (react/no-unused-state)

It seems to be specific of arrow functions as properties and that “two-level” destructuring.

It won't warn with the following:

  • straight access like this.state.state1
  • simple destructuring like const {state1} = this.state;
  • unbounded method like handleClick() {}

eslint-plugin-react: v7.2.0
eslint: v4.4.1

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions