Sub topic 3
Finding Problems with the Lint Rules
Assume you know function components, hooks, and a JavaScript build tool. This chapter shows how to find code the React Compiler cannot safely optimize and how to respond.
Finding problems with the lint rules
The React Hooks ESLint plugin reports many compiler diagnostics. These diagnostics identify code that breaks the Rules of React, such as mutating props, calling hooks conditionally, or using values during render that change unexpectedly.
Install the current eslint-plugin-react-hooks version and enable its recommended rules. The compiler diagnostics then appear beside ordinary lint errors.
// eslint.config.js
import reactHooks from 'eslint-plugin-react-hooks';
export default [
reactHooks.configs.flat.recommended,
];Treat a diagnostic as a correctness problem first. Do not hide it only to obtain compilation. For example, rendering should not mutate a prop:
function Names({ names }) {
const sorted = [...names].sort();
return <ul>{sorted.map((name) => <li key={name}>{name}</li>)}</ul>;
}The copied array preserves the caller's data and gives the compiler a predictable render.
Confirming a component is compiled
Run the application in development and open the React DevTools Components panel. Compiled components can show a Memo ✨ badge. This indicates that the compiler added memoization for that component.
Use DevTools together with lint output and a production build. A badge confirms compilation for the displayed component, but it does not prove that every child, hook, or code path was compiled.
When the compiler skips code and how to debug it
A compiler bailout means the compiler left code unchanged because it could not prove that optimization was safe. Start with the diagnostic location, then check for:
- Mutations of props, state, or other values used during render.
- Conditional or dynamically constructed hook calls.
- Impure work during render, such as changing external data.
- Unsupported library patterns or unusual JavaScript features.
Fix the smallest issue, rerun ESLint, and verify behavior before changing other code.
For gradual adoption, use 'use no memo'; to exclude a component while investigating. In annotation mode, use 'use memo'; to opt a known-safe component in.
Keeping, removing, or adding manual memoization
The compiler can coexist with memo, useMemo, and useCallback. Keep existing memoization until tests and profiling show that it is unnecessary. Remove redundant wrappers in separate, reviewable changes.
Add manual memoization when a measured performance problem remains at a boundary the compiler cannot optimize, or when an external library requires stable references. Do not add it preemptively.
Exercise
A component has a compiler diagnostic because it sorts a prop array during render. What should you do first?
Show answer
Copy the array before sorting it, rerun ESLint, and verify the component's behavior and compilation in DevTools.