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?
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.)
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.
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.
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.
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".
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.
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With