react-hooks/globals
eslint-plugin-react-hooks
import reactHooks from "eslint-plugin-react-hooks";
export default [
{
plugins: { "react-hooks": reactHooks },
rules: {
"react-hooks/globals": "error",
},
},
];Validates against assignment/mutation of globals during render, part of ensuring that side effects must render outside of render
let renderCount = 0;
function Component() {
renderCount++; // Mutating global
return <div>Count: {renderCount}</div>;
}function Component({userId}) {
window.currentUser = userId; // Global mutation
return <div>User: {userId}</div>;
}const events = [];
function Component({event}) {
events.push(event); // Mutating global array
return <div>Events: {events.length}</div>;
}function Component() {
const [clickCount, setClickCount] = useState(0);
const handleClick = () => {
setClickCount(c => c + 1);
};
return (
<button onClick={handleClick}>
Clicked: {clickCount} times
</button>
);
}function Component() {
const user = useContext(UserContext);
return <div>User: {user.id}</div>;
}function Component({title}) {
useEffect(() => {
document.title = title; // OK in effect
}, [title]);
return <div>Page: {title}</div>;
}Examples from the ESLint documentation.
react-hooks/globals
eslint-plugin-react-hooks
import reactHooks from "eslint-plugin-react-hooks";
export default [
{
plugins: { "react-hooks": reactHooks },
rules: {
"react-hooks/globals": "error",
},
},
];react/globals
react plugin · correctness
{
"plugins": [
"react"
],
"rules": {
"react/globals": "error"
}
}Biome does not have this rule.
Validates usage of error boundaries instead of try/catch for errors in child components
Validates against mutating props, state, and other values that are immutable
Validates against usage of libraries which are incompatible with memoization (manual or automatic)
Validates that existing manual memoized is preserved by the compiler. React Compiler will only compile components and hooks if its inference matches or exceeds the existing manual memoization
Validates that components/hooks are pure by checking that they do not call known-impure functions
Validates correct usage of refs, not reading/writing during render. See the "pitfalls" section in useRef() usage