sonarjs/no-hook-setter-in-body
eslint-plugin-sonarjs
No autofixRecommended
eslint.config.js
export default [
{
rules: {
"sonarjs/no-hook-setter-in-body": "error",
},
},
];React's useState hook should not be used directly in the render function or body of a component
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>
);
}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.
sonarjs/no-hook-setter-in-body
eslint-plugin-sonarjs
export default [
{
rules: {
"sonarjs/no-hook-setter-in-body": "error",
},
},
];Oxlint does not have this rule.
Biome does not have this rule.
"if/else if" chains and "switch" cases should not have the same condition
Identical expressions should not be used on both sides of a binary operator
Functions should not have identical implementations