Skip to content

Latest commit

 

History

History
60 lines (47 loc) · 1.24 KB

no-unused-state.md

File metadata and controls

60 lines (47 loc) · 1.24 KB

Disallow definitions of unused state (react/no-unused-state)

Warns you if you have defined a property on the state, but it is not being used anywhere.

Rule Details

Examples of incorrect code for this rule:

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

var UnusedGetInitialStateTest = 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} />;
  }
}

var UnusedGetInitialStateTest = createReactClass({
  getInitialState: function() {
    return { foo: 0 };
  },
  render: function() {
    return <SomeComponent foo={this.state.foo} />;
  }
})

Rule Options

This rule can take one argument to ignore some specific states during validation.

...
"react/no-unused-state": [<enabled>, { ignore: <ignore> }]
...
  • enabled: for enabling the rule. 0=off, 1=warn, 2=error. Defaults to 0.
  • ignore: optional array of states name to ignore during validation.