2

What is the difference between using curly brackets and parenthesis whne making functional components in JSX. I have a components called layout which displays props.children

But i want to know if there is a difference or a best practise between when to use what, and for what purpose.

const layout = (props) => {
<Aux>
<div>Toolbar, SideDrawer, Backdrop</div>
<main>
    {props.children}
</main>
</Aux>
}

versus

const layout = (props) => (
<Aux>
<div>Toolbar, SideDrawer, Backdrop</div>
<main>
    {props.children}
</main>
</Aux>
)
Kristoffer Tølbøll
  • 3,157
  • 5
  • 34
  • 69
  • 3
    Thie first shouldn't work because there is no `return`. – Sulthan Nov 08 '18 at 07:43
  • This was likely discussed numerous times on SO. It is specific to arrow function syntax, not React. – Estus Flask Nov 08 '18 at 07:46
  • possible duplicate of [Arrow functions and the use of parentheses () or {} or ({})](https://stackoverflow.com/questions/49425755/arrow-functions-and-the-use-of-parentheses-or-or/49425823#49425823) – Shubham Khatri Nov 08 '18 at 07:58

1 Answers1

4

Only the second snippet is correct. => (...) is implicit arrow function return. Parentheses are there for readability and consistency with multiline explicit return statement. It could be:

const layout = (props) => 
  <Aux>
  ...
  </Aux>

With proper indentation and no parentheses a hanging indent makes function harder to read.

In order for the first snippet to work, there should be explicit arrow function return:

const layout = (props) => {
  return (
    <Aux>
    ...
    </Aux>
  )
}

Notice that parentheses are needed if return and <Aux> are on different lines.

Estus Flask
  • 206,104
  • 70
  • 425
  • 565