Types of components (class vs functional)
Published by
sanya sanya
In React JS
, there are two types of components - class components
and `functional components``.
However, with the introduction of React Hooks in React 16.8, functional components have gained more capabilities and are now the preferred way of writing components in most cases.
All the type of the Components Class vs Functional are mentioned below.
Class Components
The Class components
are the classes in Javascript which extend the React.Component base class. They have a render() method that returns the JSX representation of the component's UI.
Class components
can have internal state and lifecycle methods, such as componentDidMount()
or shouldComponentUpdate()
. Prior to React 16.8, class components were the primary way of writing components with complex state and behavior.
The example of the Class Component is mentioned below -
import React from 'react';
class MyClassComponent extends React.Component {
render() {
return <div>Hello, I am a class component!</div>;
}
}
export default MyClassComponent;

Functional Components
Functional components
are JavaScript functions that return JSX. They receive props as an argument and return the component's UI based on those props. The Functional components
are the simpler and more lightweight as compared to the class components.
They don't have their own internal state or lifecycle methods by default. However, with the introduction of React Hooks, functional components
can now manage state and have lifecycle functionality using hooks like useState, useEffect, and more.
The example of the Functional Component
is mentioned in the code below -
import React from 'react';
const MyFunctionalComponent = () => {
return <div>Hello, I am a functional component!</ div>;
};
export default MyFunctionalComponent;
Since React Hooks
provide a more concise and flexible way to manage state and lifecycle in functional components, they have become the preferred choice for most React developers. Hooks allow functional components to have the same capabilities as class components but with a simpler syntax and improved code readability.
Library
WEB DEVELOPMENT
Basic
Frontend
Express JS
React
Hello World in ReactJS
Rendering Elements
States and Lifecycles in React JS
Handling Events in React JS
Introduction to JSX
Components and Props in React JS
Conditional Rendering
Lists and Keys in React JS
Introduction to Redux
Types of components (class vs functional)
Using the useEffect hook
Building custom hooks in React JS
Context API in React JS
Forms in React JS
Lifting State Up in React JS
Composition vs Inheritance
Prop drilling in React JS
Introduction to Hooks
Using the useState hook
UseMemo vs useCallback
Backend
Interview Questions
FAANG QUESTIONS
On this page
Class Components
Functional Components