react-hooks/static-components
eslint-plugin-react-hooks
import reactHooks from "eslint-plugin-react-hooks";
export default [
{
plugins: { "react-hooks": reactHooks },
rules: {
"react-hooks/static-components": "error",
},
},
];Validates that components are static, not recreated every render. Components that are recreated dynamically can reset state and trigger excessive re-rendering
function Parent() {
const ChildComponent = () => { // New component every render!
const [count, setCount] = useState(0);
return <button onClick={() => setCount(count + 1)}>{count}</button>;
};
return <ChildComponent />; // State resets every render
}function Parent({type}) {
const Component = type === 'button'
? () => <button>Click</button>
: () => <div>Text</div>;
return <Component />;
}function Parent() {
const [theme, setTheme] = useState('light');
function ThemedButton() { // Recreated every render!
return (
<button className={theme}>
Click me
</button>
);
}
return <ThemedButton />;
}const ButtonComponent = () => <button>Click</button>;
const TextComponent = () => <div>Text</div>;
function Parent({type}) {
const Component = type === 'button'
? ButtonComponent // Reference existing component
: TextComponent;
return <Component />;
}function ThemedButton({theme}) {
return (
<button className={theme}>
Click me
</button>
);
}
function Parent() {
const [theme, setTheme] = useState('light');
return <ThemedButton theme={theme} />;
}Examples from the ESLint documentation.
react-hooks/static-components
eslint-plugin-react-hooks
import reactHooks from "eslint-plugin-react-hooks";
export default [
{
plugins: { "react-hooks": reactHooks },
rules: {
"react-hooks/static-components": "error",
},
},
];react/static-components
react plugin · correctness
{
"plugins": [
"react"
],
"rules": {
"react/static-components": "error"
}
}Biome does not have this rule.
Validates against setting state during render, which can trigger additional renders and potential infinite render loops
Validates against syntax that we do not plan to support in React Compiler
Validates against calling setState synchronously in an effect. This can indicate non-local derived data, a derived event pattern, or improper external data synchronization.
Validates usage of the useMemo() hook against common mistakes. See useMemo() docs for more information.
Validates correct usage of refs, not reading/writing during render. See the "pitfalls" section in useRef() usage