The Wayback Machine - https://web.archive.org/web/20250121142313/https://www.geeksforgeeks.org/reactjs-functional-components/
Open In App

ReactJS Functional Components

Last Updated : 13 Dec, 2024
Summarize
Comments
Improve
Suggest changes
Like Article
Like
Save
Share
Report
News Follow

Functional components are widely used in ReactJS to create efficient and reusable UI elements. They are plain JavaScript functions that return React elements (JSX) to define how the UI should look. They are lightweight, simple, and capable of handling state and effects through React Hooks.

JavaScript
// App.js

import React from 'react';

function App() {

    return (
        <h1>Welcome to GeeksforGeeks</h1>
    );
}

export default App;

ReactJS Functional Components

State and Props in Functional Components

Managing State with useState Hook

Functional components utilize the useState Hook for state management.

JavaScript
import React, { useState } from "react";

function Counter() {
    const [count, setCount] = useState(0);

    const increment = () => {
        setCount(count + 1);
    };

    return (
        <div>
            <h1>Count: {count}</h1>
            <button onClick={increment}>Increment</button>
        </div>
    );
}

export default Counter;

useState: Provides a stateful value (count) and a function to update it (setCount).

Using Props in Functional Components

Props enable passing data between components.

JavaScript
function Greet({ name }) {
    return <h1>Hello, {name}!</h1>;
}

export default Greet;

In this example, the name prop customizes the greeting message dynamically.

React Lifecycle with useEffect Hook

Functional components use the useEffect Hook to handle side effects, replacing lifecycle methods like componentDidMount and componentWillUnmount.

JavaScript
import React, { useEffect } from "react";

function Timer() {
    useEffect(() => {
        console.log("Component Mounted");

        return () => {
            console.log("Component Will Unmount");
        };
    }, []);

    return <h1>Timer Running...</h1>;
}

export default Timer;
  • Mounting Phase: Code in useEffect runs after the component mounts.
  • Unmounting Phase: Cleanup code in the return function of useEffect runs before the component unmounts.

Advantages of React Functional Components

  • Simpler Syntax: No need for ES6 class structure or the ‘this’ keyword.
  • Better Performance: Functional components have a smaller memory footprint.
  • Hooks Support: Manage state and lifecycle functionalities without relying on class components.
  • Easy Testing: Pure functions are easier to test compared to class-based components.

Limitations of React Functional Components

  • Requires Understanding of Hooks: Developers need to learn Hooks for state management and side effects.
  • Compatibility Issues in Older Codebases: Some legacy React projects may still rely on class components.

React Functional Components vs. Class Components

FeatureFunctional ComponentsClass Components
SyntaxSimple function with HooksES6 classes extending React.Component.
State ManagementRequires Hooks like useStateManaged via this.state and this.setState.
Lifecycle MethodsManaged via useEffectDedicated lifecycle methods like componentDidMount.
PerformanceSlightly faster due to fewer abstractionsSlightly slower due to more internal logic.

React Functional Components – FAQs

What are React functional components?

React functional components are simple JavaScript functions that return JSX to define a component’s UI. They can manage state and lifecycle events using Hooks.

How do functional components manage state?

State in functional components is managed using the useState Hook, which provides a stateful value and a function to update it.

What replaced lifecycle methods in functional components?

The useEffect Hook replaced lifecycle methods like componentDidMount, componentDidUpdate, and componentWillUnmount.

Are functional components better than class components?

Functional components are generally preferred for new projects due to their simplicity, performance, and support for Hooks.

Are class components obsolete?

No, class components are still relevant for legacy projects and scenarios where advanced lifecycle control is necessary.



Next Article

Similar Reads

three90RightbarBannerImg