Skip to main content
GoodFirstPicks
DashboardIssuesReposLeaderboard

GoodFirstPicks by Leaveitblank © 2026

CreatorRequest a RepoPrivacy PolicyTerms of Service
Bug: react-hooks/set-state-in-effect false positive on valid example in docs | GoodFirstPicks

Bug: react-hooks/set-state-in-effect false positive on valid example in docs

facebook/react 4 comments 1mo ago
View on GitHub
mediumopenScope: somewhat clearSkill match: maybeReactJavaScriptTypeScript

Why this is a good first issue

ESLint rule incorrectly flags valid React code.

AI Summary

The issue involves a false positive from the `react-hooks/set-state-in-effect` ESLint rule, which incorrectly flags valid React code using `useLayoutEffect` with refs. The problem appears to be related to recent updates in `eslint-plugin-react-hooks`. The scope is somewhat clear, but diagnosing the root cause may require deeper knowledge of ESLint rules and React internals.

Issue Description

React version: [email protected], [email protected]

Steps To Reproduce

  1. Paste the valid sample from https://react.dev/reference/eslint-plugin-react-hooks/lints/set-state-in-effect#valid into a file.
  2. Adjust to remove type-errors when using Typescript (should be irrelevant)
  3. Perform linting with eslint-plugin-react-hooks and the react-hooks/set-state-in-effect rule enabled

Link to code example:
https://react.dev/reference/eslint-plugin-react-hooks/lints/set-state-in-effect#valid

// ✅ setState in an effect is fine if the value comes from a ref
function Tooltip() {
  const ref = useRef(null);
  const [tooltipHeight, setTooltipHeight] = useState(0);

  useLayoutEffect(() => {
    const { height } = ref.current.getBoundingClientRect();
    setTooltipHeight(height);
  }, []);
}

Typescript variant, with usage:

export function Tooltip(): ReactElement {
  const ref = useRef<HTMLDivElement>(null);
  const [tooltipHeight, setTooltipHeight] = useState(0);

  useLayoutEffect(() => {
    const height = ref.current?.getBoundingClientRect().height ?? 0;
    setTooltipHeight(height);
  }, []);

  return <div ref={ref}>{tooltipHeight}</div>;
}

Another similar example that also triggered this error:

function Foo(): ReactElement {
  const ref = useRef<HTMLDivElement>(null);
  const [isWithinBar, setIsWithinBar] = useState(false);

  useLayoutEffect(() => {
    setIsWithinBar(ref.current?.closest('.bar') != null);
  }, []);

  return <div ref={ref} className={isWithinBar ? 'variant-1' : 'variant-2'}>Foo</div>
}

The current behavior

The code, straight out of the valid section of the docs, triggers an ESLint error of react-hooks/set-state-in-effect:

Image

The expected behavior

Valid usage of useLayoutEffect to do measurements and DOM checks via refs and setting state should not generat

GitHub Labels

Status: Unconfirmed

Want to work on this?

Claim this issue to let others know you're working on it. You'll earn 10 points when you complete it!

Risk Flags

  • potential ESLint rule bug
  • dependency version conflicts
Loading labels...

Details

Points10 pts
Difficultymedium
Scopesomewhat clear
Skill Matchmaybe
Test Focusedno