Functional Components vs. Class Components in React

 

Functional Components vs. Class Components in React

In React, components can be defined using either functional components or class components. Both types serve the same purpose—to define parts of a user interface—but they have different syntax, capabilities, and use cases. Here’s a detailed comparison:


1. Syntax and Structure

Functional Components

  • Functional components are simple JavaScript functions that return JSX (JavaScript XML). They are typically used for presenting UI and can accept props as parameters.

Example:

javascript

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

Or using an arrow function:

javascript

const Greeting = (props) => { return <h1>Hello, {props.name}!</h1>; };

Class Components

  • Class components are ES6 classes that extend React.Component. They must include a render method, which returns JSX. Class components can have their own state and lifecycle methods.

Example:

javascript

class Greeting extends React.Component { render() { return <h1>Hello, {this.props.name}!</h1>; } }

2. State Management

Functional Components

  • Before Hooks: Functional components were stateless and could only receive props. They did not manage local component state.
  • With Hooks: With the introduction of React Hooks (e.g., useState, useEffect), functional components can now manage state and side effects.

Example of State in Functional Component:

javascript

import React, { useState } from 'react'; const Counter = () => { const [count, setCount] = useState(0); // State hook return ( <div> <p>You clicked {count} times</p> <button onClick={() => setCount(count + 1)}>Click me</button> </div> ); };

Class Components

  • Class components can have their own state, initialized in the constructor, and can use lifecycle methods to manage component behavior over its lifecycle.

Example of State in Class Component:

javascript

class Counter extends React.Component { constructor(props) { super(props); this.state = { count: 0 }; // Initial state } increment = () => { this.setState({ count: this.state.count + 1 }); // Updating state }; render() { return ( <div> <p>You clicked {this.state.count} times</p> <button onClick={this.increment}>Click me</button> </div> ); } }

3. Lifecycle Methods

Functional Components

  • Functional components did not have lifecycle methods before Hooks. With the introduction of hooks, you can use useEffect to replicate lifecycle behavior such as componentDidMount, componentDidUpdate, and componentWillUnmount.

Example of useEffect:

javascript

import React, { useEffect } from 'react'; const Timer = () => { useEffect(() => { const timer = setInterval(() => { console.log('Timer tick'); }, 1000); return () => clearInterval(timer); // Cleanup on unmount }, []); // Empty dependency array means it runs once after the initial render return <div>Timer is running</div>; };

Class Components

  • Class components provide built-in lifecycle methods such as componentDidMount, componentDidUpdate, and componentWillUnmount.

Example of Lifecycle Methods:

javascript

class Timer extends React.Component { componentDidMount() { this.timer = setInterval(() => { console.log('Timer tick'); }, 1000); } componentWillUnmount() { clearInterval(this.timer); // Cleanup } render() { return <div>Timer is running</div>; } }

4. Performance

  • Functional components are generally more lightweight and simpler than class components, which can lead to better performance, especially with optimization techniques like React.memo.
  • Class components may introduce overhead due to the additional complexity of maintaining state and lifecycle methods.

5. Use Cases

When to Use Functional Components:

  • For simple presentational components that only rely on props.
  • When you need to manage local state or side effects (using hooks).
  • In new React projects, as they are the recommended way to build components.

When to Use Class Components:

  • If you are maintaining older React codebases that already use class components.
  • When you need to use certain lifecycle methods that might not yet have a direct hook equivalent (though most use cases can now be handled with hooks).

Post a Comment

0 Comments