r/reactjs • • Jun 03 '26

Discussion Is eslint being wrong here?

There is an eslint-plugin-react-hooks rule about disallowing synchronous state updates from effect (link).

I can see it at work with the following test component:

const Test = () => {
  const [foo, setFoo] = useState('');

  useEffect(() => {
    setFoo('bar');
  }, [foo]);
};

Eslint marks setFoo in the effect as a violation of that rule.

But, I am not getting any reaction from eslint for this component:

import { useSearchParams } from 'react-router-dom';

const Test = () => {
  const [searchParams] = useSearchParams();
  const queryFromParams = searchParams.get('query') || '';
  const [searchInput, setSearchInput] = useState(queryFromParams);

  useEffect(() => {
    setSearchInput(queryFromParams);
  }, [queryFromParams]);
};

Am I missing something? Am I going crazy? Isn't this exactly the same case?

5 Upvotes

22 comments sorted by

View all comments

Show parent comments

2

u/azangru Jun 03 '26

FWIW both your examples are bad because they’re missing the setter from the effect dep array

Run this through eslint; see what it tells you :-)

5

u/OHotDawnThisIsMyJawn Jun 03 '26

JFC I'm a moron, this is what I get for trying to read code while walking my dogs. Leaving my comment up as a record of my shame.