React Hooks Const Component vs Functional Component

I understand the difference between a functional component and a class component, but what’s the difference between const component to a functional component?

e.g

const Home = () => {
    return (
        <div>Home</div>
    )
}

To
function Home() {
     return (
        <div>Home</div>
    )
}

Both of them can use hooks, so what’s the main difference?

Answers:

Thank you for visiting the Q&A section on Magenaut. Please note that all the answers may not help you solve the issue immediately. So please treat them as advisements. If you found the post helpful (or not), leave a comment & I’ll get back to you as soon as possible.

Method 1

There is no effective difference. First is creating a function using Arrow function expressions
syntax and storing it to a constant and the second is creating a plain function.

Both are functions that will perform the exact same task, return the component JSX code for rendering.

Also, there is no such term nor concept “Const Component”; there are “Functional Components” and “Class Components”.

Method 2

When declaring a functional component, I don’t see much of a difference between using const and simply function.However, as I have noticed, there are a few minor differences.

Const function vs Function

function Component1() {
return(
  ..
)

}
const Component = () => {
return(
  ..
)

}
  • When compared to constants, one advantage is that you can export default functions in place. You could also omit the name and export the function expression directly, but this appears to be bad practice. And using the function keyword is actually shorter than using the const keyword. You can only save some characters if your function body is simply return JSX.
  • By relying on hoisting for helper functions, if you prefer to keep your components at the bottom and use them before they are declared, we can write them with function syntax and have them hoisted to the top of the file. And we can’t do that with const.


All methods was sourced from stackoverflow.com or stackexchange.com, is licensed under cc by-sa 2.5, cc by-sa 3.0 and cc by-sa 4.0

0 0 votes
Article Rating
Subscribe
Notify of
guest

0 Comments
Inline Feedbacks
View all comments
0
Would love your thoughts, please comment.x
()
x