react-hooks/purity
eslint-plugin-react-hooks
import reactHooks from "eslint-plugin-react-hooks";
export default [
{
plugins: { "react-hooks": reactHooks },
rules: {
"react-hooks/purity": "error",
},
},
];Validates that components/hooks are pure by checking that they do not call known-impure functions
function Component() {
const id = Math.random(); // Different every render
return <div key={id}>Content</div>;
}function Component() {
const timestamp = Date.now(); // Changes every render
return <div>Created at: {timestamp}</div>;
}function Clock() {
return <div>Current time: {Date.now()}</div>;
}function Component() {
const [id] = useState(() => crypto.randomUUID());
return <div key={id}>Content</div>;
}Examples from the ESLint documentation.
react-hooks/purity
eslint-plugin-react-hooks
import reactHooks from "eslint-plugin-react-hooks";
export default [
{
plugins: { "react-hooks": reactHooks },
rules: {
"react-hooks/purity": "error",
},
},
];react/purity
react plugin · correctness
{
"plugins": [
"react"
],
"rules": {
"react/purity": "error"
}
}Biome does not have this rule.
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 correct usage of refs, not reading/writing during render. See the "pitfalls" section in useRef() usage
Validates against usage of libraries which are incompatible with memoization (manual or automatic)
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 against mutating props, state, and other values that are immutable
Validates against setting state during render, which can trigger additional renders and potential infinite render loops