react-hooks/refs
eslint-plugin-react-hooks
import reactHooks from "eslint-plugin-react-hooks";
export default [
{
plugins: { "react-hooks": reactHooks },
rules: {
"react-hooks/refs": "error",
},
},
];Validates correct usage of refs, not reading/writing during render. See the "pitfalls" section in useRef() usage
function Component() {
const ref = useRef(0);
const value = ref.current; // Don't read during render
return <div>{value}</div>;
}function Component({value}) {
const ref = useRef(null);
ref.current = value; // Don't modify during render
return <div />;
}function Component() {
const ref = useRef(null);
useEffect(() => {
if (ref.current) {
console.log(ref.current.offsetWidth); // OK in effect
}
});
return <div ref={ref} />;
}function Component() {
const [count, setCount] = useState(0);
return (
<button onClick={() => setCount(count + 1)}>
{count}
</button>
);
}function Component() {
const ref = useRef(null);
// Initialize only once on first use
if (ref.current === null) {
ref.current = expensiveComputation(); // OK - lazy initialization
}
const handleClick = () => {
console.log(ref.current); // Use the initialized value
};
return <button onClick={handleClick}>Click</button>;
}Examples from the ESLint documentation.
react-hooks/refs
eslint-plugin-react-hooks
import reactHooks from "eslint-plugin-react-hooks";
export default [
{
plugins: { "react-hooks": reactHooks },
rules: {
"react-hooks/refs": "error",
},
},
];react/refs
react plugin · correctness
{
"plugins": [
"react"
],
"rules": {
"react/refs": "error"
}
}Biome does not have this rule.
Validates that components/hooks are pure by checking that they do not call known-impure functions
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 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 against setting state during render, which can trigger additional renders and potential infinite render loops
Validates against usage of libraries which are incompatible with memoization (manual or automatic)
Validates that components are static, not recreated every render. Components that are recreated dynamically can reset state and trigger excessive re-rendering