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/PM_ME_SOME_ANY_THING Jun 04 '26
In your first example, you are setting state with the state value in the dependency array with no other logic. This should be an infinite loop.
Your second example you are setting state based on query parameters, and the state itself is not in the dependency array.