DEV Community

Cover image for Class Components vs Functional Components(React js)
Chrisspotless
Chrisspotless

Posted on

Class Components vs Functional Components(React js)

INTRODUCTION
React is a declarative, efficient, and flexible JavaScript library for building user interfaces.
It lets you compose complex UIs from small and isolated pieces of code called “components”.
In React, components are the building blocks of a UI. There are two types of components in React: class components and functional components.
In this article, we will explore the differences between class components and functional components in React and when to use each of them.

Class Components

Class components are React components that extend the React.Component class.
They have a render() method that returns a React element, which is used to describe the UI that the component should render.
Class components also have a state object that holds data that can change over time and trigger a re-rendering of the component.

Here is an example of a simple class component that displays a greeting message:


import React, { Component } from 'react';

class Greeting extends Component {
  constructor(props) {
    super(props);
    this.state = { name: 'chris' };
  }

  render() {
    return <h1>How are you, {this.state.name}!</h1>;
  }
}
Enter fullscreen mode Exit fullscreen mode

In the above example, the Greeting component extends the React.Component class and has a constructor that initializes the state object with a default name value of "chris". The render() method returns a <h1> element that displays the greeting message.

Functional Components

Functional components, also known as stateless components, are React components that are defined as plain JavaScript functions.
They take in props as input and return a React element that describes the UI that the component should render.
Functional components do not have a state object and cannot change their own state.

Here is an example of a simple functional component that displays a greeting message:

import React from 'react';

function Greeting(props) {
  return <h1>Hello, {props.name}!</h1>;
}
Enter fullscreen mode Exit fullscreen mode

In the above example, the Greeting component is defined as a plain JavaScript function that takes in props as input and returns a <h1> element that displays the greeting message.

Differences between Class Components and Functional Components
Now that we have seen examples of both class components and functional components, let's look at the differences between them:

  1. Syntax: Class components are defined using the class keyword and extend the React.Component class, while functional components are defined using a plain JavaScript function.

  2. State: Class components have a stateobject that can hold data and can change overtime aswell trigger a re-rendering of the component. Function components do not have a state object and can =not change their own state.

  3. Lifecycle methods: Class components have access to lifecycle methods, such as componentDidMount and componentWillUnmount, which can be used to perform actions before and after the component is mounted or unmounted.
    Functional components do not have access to lifecycle methods.

  4. Performance:Functional components are generally faster and more lightweight than class components because they do not have a state object or lifecycle methods.

  5. Reusability: Functional components are generally more reusable than class components because they're stateless and don't have any dependencies on other parts of your application.

  6. Context: Class components can use the React context API to share data between components, whereas functional components need to use a higher-order component or render props pattern to achieve the same result.

  7. Error boundaries: Class components can act as error boundaries, which means they can catch and handle errors that occur within their child components. Functional components can't do this, which means you need to wrap them in a separate error boundary component if you want to achieve the same effect.

When to Use Class Components

Class components are useful when you need to hold and manage state that can change over time. They are also useful when you need to access lifecycle methods, such as componentDidMount and componentWillUnmount. Class components are often used for more complex components that require more functionality.

When to Use Functional Components

Functional components are useful when you need to render a component based on props without managing state. They are also useful when you want to write a more lightweight and performant component. Functional components are often used for simpler components that only need to display information.

Conclusion

Both React class components and functional components have their own unique strengths and weaknesses. Class components are more suitable for managing state and accessing lifecycle methods, while functional components are faster and more lightweight, making them ideal for simpler components that do not require state management.

When choosing between class components and functional components, it's essential to the specific requirements of your project. If your component requires state management and lifecycle methods, a class component may be more appropriate. On the other hand, if you need a more lightweight and reusable component, a functional component may be a better choice.

Ultimately, the decision between class components and functional components comes down to the individual needs of your project and the preferences and experience of your development team. Regardless of which approach you choose, React provides a flexible and powerful framework for building robust and dynamic user interfaces.

Top comments (1)

Collapse
 
yashpataliya01 profile image
Yash pataliya

i have seen that now days no body using class components?
is that make any difference