mqklin
mqklin

Reputation: 2048

Is there eslint-react rule that forces to use functional setState only?

I actively use eslint-plugin-react and recently read about functional setState. I wanted to use eslint rule for this, but couldn't find. Is there eslint-react rule that forces to use functional setState only?

Upvotes: 2

Views: 273

Answers (1)

Short answer

Currently, there is no such eslint-plugin-react rule available.

Additional information

Related issues

There are the related GitHub issues:

  1. Rule idea: Disallow accessing this.state within setState · Issue #943 · yannickcr/eslint-plugin-react.
  2. Rule Suggestion : setState function · Issue #1196 · yannickcr/eslint-plugin-react.
  3. Rule proposal: Require/Disallow passing an object to setState() if not using previous state · Issue #1836 · yannickcr/eslint-plugin-react.

Please, take a look at them.

«Closest» issue

The issue #2 from the «Related issues» list — Rule Suggestion : setState function · Issue #1196 · yannickcr/eslint-plugin-react — seems to be the closest to or the same as your question (request).

The implementation-related issue: Rule idea: Disallow accessing this.state within setState · Issue #943 · yannickcr/eslint-plugin-react.

It seems the feature was implemented somewhat partially.
Partially, because:

  1. It disallows only the use of this.state within the setState() argument: both object initialiser and function.
  2. While it still allows passing an object (without using this.state in its object initialiser) as the setState() function argument. For example:
    this.setState({quantity: 2});
    

Please, consider reporting the details to the existing «closest» issue to make an attempt to re-open it or open a new issue.

Upvotes: 2

Related Questions