0

I couldn't find a proper answer for that, but I'd like to know if it is a bad practice or a recommendation related to using the matching state variable to the state setter function.

setMyState(!myState);

It's common to see this approach mainly to change the boolean values to the opposite. So I thought it was normal to do it.

But I'm getting this error from Sonar:

React state setter function should not be called with its matching state variable

When the setter function is called with the state variable as a parameter, nothing will happen. This can happen by mistake when attempting to reset a default value or invert a boolean, among others.

The fix is simple, but I've never seen any warning or official information related to it. Does anyone?

KarolHarumi
  • 109
  • 1
  • 5

0 Answers0