DEV Community

Cover image for React functional components: const vs. function

React functional components: const vs. function

Carl-W on June 03, 2020

I have been performance optimizing our app recently and as such, I have been getting into the nitty gritty of Javascript. One of the things I thoug...
Collapse
 
rhiannonmonks profile image
Rhiannon Monks

Thanks, this is a really helpful, succinct, well written article.

Collapse
 
ugglr profile image
Carl-W

Thank you πŸŽ‰

Collapse
 
iambilalriaz profile image
Hafiz Muhammad Bilal

What an explanation mate!
Thanks and keep sharing such amazing stuff.

Collapse
 
ugglr profile image
Carl-W

Glad I could help πŸ€“

Collapse
 
sargnec profile image
Necmettin SargΔ±n

My component was not showing up const MyComponent = () => {...} bc of this and its not gives error or something I searched what I'm doing wrong and finally saw that if u use { } u have to use return otherwise (...) . Thank you

Collapse
 
doctorderek profile image
Dr. Derek Austin πŸ₯³

That's a fantastic tip, and I've also been tripped up by that particular thing. You also sometimes need () around the JSX, though Prettier will add it for you. I wrote an article about this topic: javascript.plainenglish.io/how-to-...

Collapse
 
doctorderek profile image
Dr. Derek Austin πŸ₯³

I prefer "const" over function, but I don't like retyping the name of the component twice.

It's a tiny bit easier to write:

export default function Component() {
  return <>Yoooo</>
}
Enter fullscreen mode Exit fullscreen mode

Than it is to write:

const Component() = (<>Yoooo</>)

export default const Component
Enter fullscreen mode Exit fullscreen mode

Thanks for the nice article! 😁

Collapse
 
johnmaxwelldistinti profile image
John Distinti

Interesting distinction, I'll keep this in mind when using const vs. function. Also, I appreciate the Dude Where's My Car joke!

Collapse
 
ugglr profile image
Carl-W

Awesome with another DWMC-fan! Thanks for reading ^_^

Collapse
 
gsmoon97 profile image
Geonsik Moon

thanks for the homemade analysis πŸ‘

Collapse
 
segnova profile image
Joshua Segura

Thank you

Collapse
 
samx23 profile image
Sami Kalammallah

Good to know

Collapse
 
doctorderek profile image
Dr. Derek Austin πŸ₯³

"And then? (any Dude where's my car fans here?)" Awesome! Thanks for exactly the article I needed! πŸ™πŸ¦ƒ

Collapse
 
ugglr profile image
Carl-W

Haha finally! Thank you!