#Difference between return setState and setState then return

8 messages · Page 1 of 1 (latest)

ocean canyon
#

Is there a difference between the way this function returns? It works but I'm more so asking whether there are any performance differences between:

const someFunction = () => {
  if(someVal){
    return setState([...products, newProduct])
  }
 //... other stuff
}

Or

const someFunction = () => {
  if(someVal){
    setState([...products, newProduct])
    return
  }
  //... other stuff
}
gritty flume
#

setState is of type (value: React.SetStateAction<string>) => void
meaning it doesn't return anything (technically speaking, a function with return; returns undefined but let's ignore that)

#

so doing setState(); return; is the exact same as doing return setState();

#

there is no difference in logic or performance

#

it basically comes down to your own code style

#

the former being explicit about returning nothing,
the later being shorter (everything one one line) but a bit more ambiguous (especialyl if you don't know the exact type of setState)

#

@ocean canyon

ocean canyon
#

Alright I was just wondering, thanks. Inb4 there's some underlying thing here