Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

ReactJS: what is the difference between functional component and class component

Could anyone can explain in detail the difference between functional component and class component in ReactJS?

When we use a functional component and when we use the class component?

like image 374
Hung Tran Avatar asked Mar 12 '16 04:03

Hung Tran


People also ask

Which one is better class or functional component in React?

Personally, I found the functional component easier to understand compared to the class component, although this might be different for a developer from object-oriented programming like Java. The class component uses ES6 class syntax, and it extends React components with a render method that returns React elements.

When should we use a functional component vs a class component?

In the case of class components, when the state of the component is changed then the render method is called. Whereas, function components render the interface whenever the props are changed.

Why use functional components over class components React?

Functional component are much easier to read and test because they are plain JavaScript functions without state or lifecycle-hooks. You end up with less code. They help you to use best practices.


2 Answers

Functional Components

A functional component is basically a JavaScript function which returns a React element. Its accepts props as argument and returns valid JSX

Class Components

Class Components are more complex than functional components including constructors, life-cycle methods, render( ) function and state (data) management. Class components are ES6 classes.

Benefits of using Functional Components

  1. Functional components are easy to test.
  2. It can have better performance.
  3. Functional components are easy to debug.
  4. It end up with less code.
  5. It help you to use best practices.
  6. Functional components can reduce coupling.

For more click here

like image 64
Soni Kumari Avatar answered Oct 17 '22 06:10

Soni Kumari


See this picture. I am too much late but i will help the others.

Source of Image is Udemy Course

enter image description here

like image 31
Muhammad Usman Avatar answered Oct 17 '22 04:10

Muhammad Usman