useEffect without dependencies
This is how the comment reads inline, under the flagged line.
Why it matters
An effect with no dependency array runs after every render. It's a common source of loops and wasted work.
What to do
List the values it reads, or [] to run once.
Examples
useEffect(() => { track(page); });useEffect(() => { track(page); }, [page]);What it catches
It reads the shape of the code: functions, blocks, loops and calls. Languages: TypeScript and JavaScript. Scope: All files. These cases are the rule's own tests: the first set must be flagged and the second must not.
Flagged
useEffect(() => {
track(page);
});
Left alone
useEffect(() => {
track(page);
}, [page]);
Research
Effects with no dependency array, data fetching in effects, and effects that copy derived values into state. They cause extra renders, loops, races and double fetches.
- X post by aidenybai
- skill: react-doctor
- owner's notes
Turn it off or change it
In a config file for the CLI, or in the API's rules field:
{
"rules": {
"disable": [
"effect-no-deps"
],
"severity": {
"effect-no-deps": "low"
}
}
}
useEffectwith no dependency array runs after every render.