Sub topic 10
Finding problems with the lint rules
This chapter assumes you can write function components with useState and useEffect, and that you know the Rules of React from the earlier chapter: render must be pure, and props and state are never mutated. It also assumes you have an ESLint setup. The compiler skips code that breaks those rules. The lint rule finds that code before you ship it.
What the rule does
The compiler's analysis is available as an ESLint rule. It runs the same checks the compiler runs, but it reports each problem as a lint error with a file and line number. You see the problems in your editor, and you don't have to wonder why a component was skipped.
The rule works without the Babel plugin installed in your build. You can adopt it first, fix violations, and enable the compiler later. This is a low-risk way to find out how ready your codebase is.
Installing the rule
Install the plugin as a development dependency:
npm install --save-dev eslint-plugin-react-compilerThen enable it in a flat config file:
// eslint.config.js
import reactCompiler from 'eslint-plugin-react-compiler';
export default [
{
plugins: { 'react-compiler': reactCompiler },
rules: { 'react-compiler/react-compiler': 'error' },
},
];Newer releases of eslint-plugin-react-hooks ship these checks in their recommended presets. If your project already uses that plugin, check its documentation for the current preset name before adding a second plugin.
Reading a diagnostic
Here is the mutation example from the Rules of React, which the rule reports:
// Breaks the Rules of React: mutates a prop during render.
function Bad({ items }) {
items.sort();
return <ul>{items.map((i) => <li key={i}>{i}</li>)}</ul>;
}items.sort() sorts the array in place. The array belongs to the parent, so rendering Bad changes data it does not own. The compiler cannot safely cache the result of a render that has side effects, so it would skip this component. The lint rule reports the line and says the value is a prop that must not be modified.
The fix is to copy first:
function Good({ items }) {
const sorted = [...items].sort();
return <ul>{sorted.map((i) => <li key={i}>{i}</li>)}</ul>;
}After this change the rule reports nothing, and the compiler can optimize the component.
Common violations
| Pattern | Why it is flagged | Typical fix |
|---|---|---|
| Mutating props or state in render | Render must be pure | Copy, then change the copy |
| Writing to a variable declared outside the component during render | Hidden side effect | Move the write into an event handler or effect |
| Reading or writing `ref.current` during render | Refs are not tracked as reactive values | Read refs in handlers or effects |
| Calling hooks conditionally or in loops | Breaks the Rules of Hooks | Move the hook to the top level |
| Mutating a value after passing it to JSX or a hook | The value may already be cached | Finish building the value before using it |
Where the rule fits
If a fix is too large to make now, opt the component out with 'use no memo'. The directive stops the compiler from touching the function, but you should still track the violation as a to-do.
Mistakes to avoid
- Do not silence the rule with
eslint-disableand keep the compiler on. The compiler would skip the code anyway, or in rare cases compile something with behavior you did not expect. - Do not treat a clean lint run as proof that a component is compiled. The rule checks that code is safe to compile. It does not show what was compiled. The next chapters cover DevTools verification.
- Do not ignore warnings on files you did not write. Third-party patterns that mutate during render show up here too.
Exercise
A component calls props.list.reverse() during render, and the lint rule reports it. What is the problem, and what is the smallest fix?
Show answer
reverse() reorders the array in place, which mutates a prop during render and breaks purity, so the compiler cannot safely memoize the component. Copy the array before reversing it: const reversed = [...props.list].reverse();.
Exercise
You cannot fix a flagged component this week. How do you keep the rest of the project compiled without hiding the problem?
Show answer
Add the 'use no memo' directive to that component so the compiler skips only it. Leave the lint error in place or track it in your issue list, so the violation gets fixed later.