Home > Back-end >  Return content without parentheses
Return content without parentheses

Time:10-30

I'm using Next.JS (13) and I was wondering if there is any problem in writing the code like this: not using parentheses

Instead of: using parentheses

Is there any difference between the results?

I tested both but I didn't see any difference, I started using React a short time ago, and I can't say if in larger projects there is any difference...

CodePudding user response:

From the perspective of the 'react compiler', there is no difference as in both cases you return a React.Fragment object.

However, from my experience, when using javascript there are a couple of situations when you want to use parentheses.

For example, when you want to have a lambda function that returns an object:

()=>{prop:'My Object'} // syntax error as the lambda is considering the object declaration as a function body

()=>({prop:'My Object'}) // works

Or, some vscode extensions might be confused about not using parentheses, so for example, one might autocomplete and put a ';' in the wrong place:

  ()=><p> ;            // semicolon in the wrong place
     Some text </p>

Hope it helps!

  • Related