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
15
u/itsAbhinav_5383 Jun 03 '26
Apologies if I'm wrong, but aren't these two different?
in the first one you're calling the setter of a state which is in the dependency array (which can cause an infinite re-render)
but in the second one the dependency is different thing, which is fine. There are valid usecases for that
27
u/kwietog Jun 03 '26
You don't need useEffect there. I'm not sure who is teaching you to set state inside an effect but they are wrong. Give them this to read: https://react.dev/learn/you-might-not-need-an-effect (and you should read it too).
4
u/devenitions Jun 03 '26
If you setFoo, foo changes. If you setSearchInput, searchInput changes, queryFromParams remains as is.
On top of it, queryFromParams is (likely) returned from a function where your first example you use a string.
1
u/PM_ME_SOME_ANY_THING Jun 04 '26
Your Foo example and the example in the link you provide are different.
This is the example in the link you provide, and it sets a state value to a prop, when it could just initialize state with the prop.
10
u/United-Consequence47 Jun 03 '26
You're not going crazy, it's the same pattern and the rule is just failing to catch it. Static analysis tools use heuristics, and this one likely tracks `useState` setters by looking for simple destructuring with a literal or empty initializer. Once you pass a derived value like `queryFromParams` as the initial state, the rule loses track of the binding.
That said, the second example is arguably more "intentional looking" to a linter : the effect is syncing state to an external source (URL params) which is a recognized pattern, even if debatable. The first one is a more obvious loop signal (state updating itself in its own dep array).
Either way you're right to be suspicious. Both patterns have the same risk : a render cycle if the deps aren't stable. For the URL params case, the cleaner approach is to not duplicate the state at all :
- If searchInput is always derived from queryFromParams, just use queryFromParams directly (no useState)
- If the user can also modify searchInput independently from the URL, use a controlled input but initialize once with a ref or a lazy useState initializer, and drop the effect entirely
The effect + setState pattern for syncing to URL params is one of those things that "works" but creates subtle bugs when the dep chain gets more complex ^^
1
u/climber_sean Jun 03 '26 edited Jun 03 '26
I might be wrong, but in the second example youre setting the state to the same value is it initialised with, so react will bail out and not cause a rerender. This could possibly why the linter doesn't care
Edit: https://react.dev/reference/react/useState in the caveats section this is mentioned
1
u/jiminycrix1 Jun 06 '26
Open an issue on the linter plugin instead of asking here lol the maintainers should be able to tell. If it’s not a bug no harm done. If it is - then thanks for helping make open source better.
-2
u/OHotDawnThisIsMyJawn Jun 03 '26 edited Jun 03 '26
FWIW both your examples are bad because they’re missing the setter from the effect dep array
You should always have exhaustive deps turned on
Edit: jfc I'm a moron, will leave my comment up for shame