0

In React (not really relevant to my question), I have the following:

const Curious = true;
useEffect(() => {
  Curious && initSearch();
}, [Curious, initSearch]);

I'm getting "Expected an assignment or function call and instead saw an expression" on Curious && initSearch(); But that line should be equivalent to writing the following, which doesn't give me the warning:

const Curious = true;
useEffect(() => {
  if (Curious) { initSearch() };
}, [Curious, initSearch]);

What am I missing?

Ben
  • 2,957
  • 2
  • 27
  • 55
  • Yes, they're equivalent, and you shouldn't write the misleading `&&` expression but the proper `if` statement. – Bergi Aug 05 '20 at 22:21
  • @Bergi if they are equivalent, then why is `&&` misleading? – Ben Aug 05 '20 at 22:22
  • 1
    Because it's unidiomatic, and not even a lot shorter. A `&&` produces a value, which you don't want here, so you shouldn't use it (it's even worse in [code like this](https://stackoverflow.com/q/23183972/1048572)). That's what linter style warnings are all about. – Bergi Aug 05 '20 at 22:24

0 Answers0