redline
MediumReacteffect-no-deps

useEffect without dependencies

useEffect without dependencies. useEffect with no dependency array runs after every render.

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

Flagged
useEffect(() => { track(page); });
Better
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.

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"
    }
  }
}

Read the source or report a false positive.