Open In App

Why it is Recommended to use Functional Components over Class Components ?

In this article, we will learn about the use of the functional component over the class component. In React JS, there are two main types of components: 

Functional Components are JavaScript functions that take in props as an argument and return a React element to be rendered. They are simpler and less verbose than class components and can be easier to reason about and test. They are also generally faster to render.



Class Components, on the other hand, are JavaScript classes that extend React.Component class. They have more features and capabilities than functional components, such as state and lifecycle methods. They are also useful for more complex components, such as ones that need to manage an internal state or handle user events.

Reasons to use Functional Component over Class Components

There are a few reasons why it’s recommended to use functional components over class components in React:



Steps to Create the React Application And Installing Module:

Step 1: To create a react app, install react modules by using npx command.

npx create-react-app project name

Step 2: After creating your react project, move into the folder to perform different operations by using the below command.

cd project name

Project Structure:

Project Structure

The updated dependencies in package.json file will look like:

"dependencies": {
"react": "^18.2.0",
"react-dom": "^18.2.0",
"react-scripts": "5.0.1",
"web-vitals": "^2.1.4",
}

 Example 1: React functional components are defined using ES6 arrow functions or function declarations. The code used below is in the FunctionalComponent.js file that will be present in the Component folder. Here’s an example of a basic functional component in React:




import './App.css';
import FunctionalComponent from
    './Component/FunctionalComponent';
 
function App() {
    return (
        <div className="App">
            <FunctionalComponent name="GFG" />
        </div>
    );
}
 
export default App;




import React from 'react'
 
const FunctionalComponent = (props) => {
    return (
        <div>
            <h1>
                This is an example of
                Functional Component.
            </h1>
            <p>
                name passed from App Component to
                Functional Component using props
            </p>
 
            <h2>{props.name}</h2>
        </div>
    )
}
 
export default FunctionalComponent;

Step: Open the terminal and type the following command to host the project on the local server.

npm start

Output:

Both the functional declaration and arrow function code will have the same output

Steps to convert any Class Component to a Functional Component:

Example 2: Below is the code for the App.js file which will remain the same for both class and functional components.




// Below is the code for App.js file and
// it will remain same for both class
// component and functional component.
import './App.css';
import MyComponent from './Component/MyComponent';
 
function App() {
    return (
        <div className="App">
            <MyComponent name="GFG-Counter" />
        </div>
    );
}
 
export default App;




// Class component
import React, { Component } from 'react';
 
class MyComponent extends Component {
    constructor(props) {
        super(props);
        this.state = {
            count: 0,
        };
    }
 
    componentDidMount() {
        console.log('Component mounted');
    }
 
    handleClick = () => {
        this.setState({ count: this.state.count + 1 });
    };
 
    render() {
        return (
            <div>
                <h1>{this.props.name}</h1>
                <p>Count: {this.state.count}</p>
                <button onClick={this.handleClick}>
                    Click me
                </button>
            </div>
        );
    }
}
 
export default MyComponent;

Step: Open the terminal and type the following command to host the project on the local server.

npm start

Output:

Output


Article Tags :