Back to blog

October 06, 2023

avataravatar

Gautam Patoliya, Deep Poradiya

Tutor Head

Why function components over the class components

blog-img-Why function components over the class components


Function Component and Class Component both are Components of React. Components are the heart of React. Components codes we can use many times. Those are independents. Components are basic building blocks of React, and they represent classes or functions that accept input and render the various HTML elements. The best use is we can write in this HTML. It uses JSX to write HTML in React. It is known as JavaScript XML and also known as JavaScript Syntax Extension. It makes the work of developers easier. If you want to get more information on JSX then you can visit our blog Introduction of JSX.




Let's First get the Function Component and Class Component basic information. First, we need to know what the Class component is and what the Function component is.


1) Class Component

Class Components are JavaScript classes that extend React. Component class. They have more capabilities and more features compared to the Function Component. It easily manages the internal state and handles user events.


2) Function Component

Function Component is a simple function of JavaScript. It takes props as an argument and returns React elements. They are easy compared to Class Components. And also faster to render. If the function component was weak compared to the class component, hooks were created to make it strong. With the help of which function component class can work like a component.




Let's Discuss why the Function Component is over the Class Component.


Less Code:

In the Function Component need to write less code compared to the Class Component. A function component contains code that is easy to understand. So we can understand it easily.


Simplicity:

Function Component is simpler. As we can see above, it has an easily understandable code.


Performance:

The function Component is faster compared to the Class Component.


State Management:

Class components support state management. but function components do not support state management. so, React provides a hook, useState() for the function components to maintain its state.


Hooks:

The class component had more capabilities than the function component but hooks were used to make the function component powerful. Now all the work can be done from the function component, which was done only from the class component earlier.


Life cycle:

Class components have a life cycle. The function component does not have a life cycle. Again, React provides a hook, useEffect() for the function component to access different stages of the component.





Rules for using both Components

must be written in the Component's names of the first word in Upper case.

Lets, we can see the basic syntax examples of both components.




Creating class-based components


class Demo extends React.Component {
    render() 
    {
         return <h1> Hello World</h1>
    }
} 



Creating function-based components


function Demo(props){
     return <h1> Hello World</h1>
} 




In the above example, The first is a class component and the second one is a function component example. I am taking the same name Demo for both functions. I'm writing Demo's First word in capital. That is the right rule for components. If we write the first word of the element in lowercase, it throws an error.


This keyword inside the constructor function refers to an object that consists of all the properties and methods declared using this keyword.


function component syntax is based on simple javascript syntax.





Advantages of Function Components


1. Function Components are easy to understand.

2. Function Components have less code compared to class components.  

3. Function Components did not use the render method but the class is used.  

4. Function Components use hooks.  





React version 18 provides 15 hooks for developers

  1. useCallback
  2. useContext
  3. useDebugValue
  4. useDeferredValue
  5. useEffect
  6. useId
  7. useImperativeHandle
  8. useInsertionEffect
  9. useLayoutEffect
  10. useMemo
  11. useReducer
  12. useRef
  13. useState
  14. useSyncExternalStore
  15. useTransition


  

 

 

 

 

ReactJs