# no-hook-setter-in-body

Rule `sonarjs/no-hook-setter-in-body`. React's useState hook should not be used directly in the render function or body of a component

- 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: #1279 by reach, #580 by growth

## Linters

| Linter | Rule name | Recommended | Autofix | Details | Docs |
| --- | --- | --- | --- | --- | --- |
| ESLint | `sonarjs/no-hook-setter-in-body` | yes | no | package `eslint-plugin-sonarjs` | [docs](https://rules.sonarsource.com/javascript/RSPEC-6442/) |

## Code examples

### Incorrect: the rule reports this

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

function ShowLanguage() {
    const [language, setLanguage] = useState("fr-FR");

    // Makes an infinite loop
    setLanguage(navigator.language);

    return (
      <section>
        <h1>Your language is {language}!</h1>
        <button onClick={() => setLanguage("fr-FR")}>Je préfère le Français</button>
      </section>
    );
}
```

### Correct: the rule accepts this

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

function ShowLanguage() {
    const [language, setLanguage] = useState(navigator.language);

    return (
      <section>
        <h1>Your language is {language}!</h1>
        <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-hook-setter-in-body": "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-hardcoded-secrets](https://lintradar.dev/rules/sonarjs/no-hardcoded-secrets.md): Secrets should not be hard-coded
- [sonarjs/no-identical-conditions](https://lintradar.dev/rules/sonarjs/no-identical-conditions.md): "if/else if" chains and "switch" cases should not have the same condition
- [sonarjs/no-hardcoded-passwords](https://lintradar.dev/rules/sonarjs/no-hardcoded-passwords.md): Credentials should not be hard-coded
- [sonarjs/no-identical-expressions](https://lintradar.dev/rules/sonarjs/no-identical-expressions.md): Identical expressions should not be used on both sides of a binary operator
- [sonarjs/no-hardcoded-ip](https://lintradar.dev/rules/sonarjs/no-hardcoded-ip.md): IP addresses should not be hardcoded
- [sonarjs/no-identical-functions](https://lintradar.dev/rules/sonarjs/no-identical-functions.md): Functions should not have identical implementations

---

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