Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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?

like image 501
danivegas Avatar asked Apr 18 '20 17:04

danivegas


People also ask

Can we use React Hooks in functional component?

Hooks are functions that let you “hook into” React state and lifecycle features from function components. Hooks don't work inside classes — they let you use React without classes. (We don't recommend rewriting your existing components overnight but you can start using Hooks in the new ones if you'd like.)

Which is better functional component or class component in React?

Nothing is better, because both have pros and cons. But class components are important to understand React flow and lifecycle methods. The new learner should practice React using class components. Once they are familiar with class components, they can learn and use functional components.

Is React Hooks better than class components?

Hooks make React so much better because you have simpler code that implements similar functionalities faster and more effectively. You can also implement React state and lifecycle methods without writing classes. Below are code examples to illustrate React class and functional components.

Why React hook useState uses const and not let?

useState is simply a state updating function. Const is used here because the change of value is being managed somewhere else by React. You're telling React to manage some value for you by calling useState.


2 Answers

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".

like image 83
Christos Lytras Avatar answered Oct 13 '22 19:10

Christos Lytras


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.

like image 37
Dinindu Gunathilaka Avatar answered Oct 13 '22 20:10

Dinindu Gunathilaka