react-hooks/use-memo
eslint-plugin-react-hooks
import reactHooks from "eslint-plugin-react-hooks";
export default [
{
plugins: { "react-hooks": reactHooks },
rules: {
"react-hooks/use-memo": "error",
},
},
];Validates usage of the useMemo() hook against common mistakes. See useMemo() docs for more information.
function Component({ data }) {
const processed = useMemo(() => {
data.forEach(item => console.log(item));
// Missing return!
}, [data]);
return <div>{processed}</div>; // Always undefined
}function Component({user}) {
// No return value, just side effect
useMemo(() => {
analytics.track('UserViewed', {userId: user.id});
}, [user.id]);
// Not assigned to a variable
useMemo(() => {
return analytics.track('UserViewed', {userId: user.id});
}, [user.id]);
}function Component({ data }) {
const processed = useMemo(() => {
return data.map(item => item * 2);
}, [data]);
return <div>{processed}</div>;
}function Component({user}) {
const handleClick = () => {
analytics.track('ButtonClicked', {userId: user.id});
// Other click logic...
};
return <button onClick={handleClick}>Click me</button>;
}function Component({theme}) {
useEffect(() => {
localStorage.setItem('preferredTheme', theme);
document.body.className = theme;
}, [theme]);
return <div>Current theme: {theme}</div>;
}Examples from the ESLint documentation.
react-hooks/use-memo
eslint-plugin-react-hooks
import reactHooks from "eslint-plugin-react-hooks";
export default [
{
plugins: { "react-hooks": reactHooks },
rules: {
"react-hooks/use-memo": "error",
},
},
];react/use-memo
react plugin · correctness
{
"plugins": [
"react"
],
"rules": {
"react/use-memo": "error"
}
}Biome does not have this rule.
Validates against syntax that we do not plan to support in React Compiler
Validates that components are static, not recreated every render. Components that are recreated dynamically can reset state and trigger excessive re-rendering
Validates against setting state during render, which can trigger additional renders and potential infinite render loops
Validates against calling setState synchronously in an effect. This can indicate non-local derived data, a derived event pattern, or improper external data synchronization.