0

I'm new to React and I often see the usage of React's useState like the following line:

setComment && setComment('Comment');

Why would you do that with the same useState function?

Thanks in advance!

Reut
  • 27
  • 2
  • https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Operators/Logical_AND – 0stone0 Jan 20 '22 at 10:32
  • Does this answer your question? [What does this symbol mean in JavaScript?](https://stackoverflow.com/questions/9549780/what-does-this-symbol-mean-in-javascript) – 0stone0 Jan 20 '22 at 10:32
  • if `setComment` is a state function then `setComment && setComment(x)` is pointless unless you have somehow unset the function to be undefined/null etc as it should always be a function and therefore checking it's existence is not needed – andy mccullough Jan 20 '22 at 10:34
  • This is more of a JavaScript question rather than a React question. Basically what this does is it ensures that setComment is defined before trying to call it. – Dimitar Jan 20 '22 at 10:36
  • Yes, but the OP specifically mentions react state function – andy mccullough Jan 20 '22 at 10:38
  • @0stone0 It helps a bit but not in React's context and it doesn't answer the question why you would do such a thing with the same state function. Nevertheless, thanks! – Reut Jan 20 '22 at 17:25
  • @DimitarVeljanovski I get the point thanks! – Reut Jan 20 '22 at 17:26
  • @andymccullough Your answer perfectly answers my question, thanks a lot! – Reut Jan 20 '22 at 17:27

0 Answers0