# no-useless-react-setstate

Rule `sonarjs/no-useless-react-setstate`. React state setter function should not be called with its matching state variable

- Source: sonarjs
- Technologies: JavaScript
- Kind: correctness
- Needs type information: no
- Autofix: no
- Reach: 6.2M weekly downloads of presets that enable it
- Trend: +62.1% last quarter
- Rank: #1331 by reach, #632 by growth

## Linters

| Linter | Rule name | Recommended | Autofix | Details | Docs |
| --- | --- | --- | --- | --- | --- |
| ESLint | `sonarjs/no-useless-react-setstate` | yes | no | package `eslint-plugin-sonarjs` | [docs](https://rules.sonarsource.com/javascript/RSPEC-6443/) |

## Code examples

### Incorrect: the rule reports this

```js
import { useState } from "react";

function ShowLanguage() {
    const [language, setLanguage] = useState("fr-FR");
    return (
      <section>
        <h1>Your language is {language}!</h1>
        <button onClick={() => setLanguage(navigator.language)}>Detect language</button>
        <button onClick={() => setLanguage(language)}>Je préfère le Français</button>{/* Non compliant: This button does nothing */}
      </section>
    );
};
```

### Correct: the rule accepts this

```js
import { useState } from "react";

function ShowLanguage() {
    const [language, setLanguage] = useState("fr-FR");
    return (
      <section>
        <h1>Your language is {language}!</h1>
        <button onClick={() => setLanguage(navigator.language)}>Detect language</button>
        <button onClick={() => setLanguage("fr-FR")}>Je préfère le Français</button>
      </section>
    );
};
```

Examples from the ESLint documentation.

## Enable it

### ESLint: `eslint.config.js`

```js
export default [
  {
    rules: {
      "sonarjs/no-useless-react-setstate": "error",
    },
  },
];
```

## Presets that enable it

| Preset | Linter | Package | Rules | Weekly downloads | Trend |
| --- | --- | --- | --- | --- | --- |
| [SonarJS recommended](https://lintradar.dev/presets/sonarjs-recommended.md) | ESLint | `eslint-plugin-sonarjs` | 230 | 6.2M | +62.1% |

## Related rules

- [sonarjs/no-useless-intersection](https://lintradar.dev/rules/sonarjs/no-useless-intersection.md): Type intersections should use meaningful types
- [sonarjs/no-vue-class-component](https://lintradar.dev/rules/sonarjs/no-vue-class-component.md): Vue components should not use the deprecated "vue-class-component" or "vue-property-decorator" libraries
- [sonarjs/no-useless-increment](https://lintradar.dev/rules/sonarjs/no-useless-increment.md): Values should not be uselessly incremented
- [sonarjs/no-vue-mixins](https://lintradar.dev/rules/sonarjs/no-vue-mixins.md): Vue components should not use mixins
- [sonarjs/no-useless-catch](https://lintradar.dev/rules/sonarjs/no-useless-catch.md): "catch" clauses should do more than rethrow
- [sonarjs/no-weak-cipher](https://lintradar.dev/rules/sonarjs/no-weak-cipher.md): Cipher algorithms should be robust

---

Web page: https://lintradar.dev/rules/sonarjs/no-useless-react-setstate. Data from npm and the linters' documentation, collected Dec 4, 2026. Site guide for agents: https://lintradar.dev/llms.txt
