close

no-unused-state

Configuration

rslint.config.ts
import { defineConfig, reactPlugin } from '@rslint/core';

export default defineConfig([
  reactPlugin.configs.recommended,
  {
    rules: {
      'react/no-unused-state': 'error',
    },
  },
]);

Rule Details

Warns when a React component defines state fields that are never read anywhere in the component. State can be defined via constructor assignments (this.state = {}), class property declarations (state = {}), this.setState() calls, or getInitialState() returns in ES5 components.

Examples of incorrect code for this rule:

class MyComponent extends React.Component {
  state = { foo: 0 };
  render() {
    return <SomeComponent />;
  }
}
var MyComponent = createReactClass({
  getInitialState: function() {
    return { foo: 0 };
  },
  render: function() {
    return <SomeComponent />;
  }
});

Examples of correct code for this rule:

class MyComponent extends React.Component {
  state = { foo: 0 };
  render() {
    return <SomeComponent foo={this.state.foo} />;
  }
}
class MyComponent extends React.Component {
  state = { foo: 0 };
  render() {
    const { foo } = this.state;
    return <SomeComponent foo={foo} />;
  }
}

Original Documentation