Home

void over curly braces

Gunar Gessner

Gunar Gessner

Apr 1, 2021

I disencourage returning values that aren't supposed to be returned:

  // Don't do this
  onCopy={() => setCopied(true)}

The function above returns the result of setCopied(), which in the case of React is most likely undefined and shouldn't create problems. However, what if the code behind onCopy changes to expect certain things to be returned?

Better to only return expected values:

  onCopy={() => {
    setCopied(true);
  }}

The code above explicitly returns undefined, even if setCopied ever changes.

And you can get rid of the curly braces by using the void keyword:

  onCopy={() => void setCopied(true)}

Sign up for the newsletter


Read other stuff