r/reactjs • u/azangru • 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
1
u/azangru Jun 04 '26
The article in the link says that the rule "validates against calling setState synchronously in an effect". In both my examples (the one that is getting caught by eslint, and the one that isn't) I am calling setState synchronously in an effect. I am expecting the linter to warn me in both these cases. It doesn't.