Introduction
When building web applications, developers often need to decide between using class components or functional components. This choice is crucial as it influences the code's structure, performance, and ease of maintenance.

This article will discuss both types of components, explaining their syntax with proper examples, and the differences. This topic is one of the crucial aspect which eventually going to help us in making the code structure.
Functional Components
Functional components in React are simple JavaScript functions. These functions accept props as an argument and return React elements that describe what should appear on the screen. They do not use the this keyword, making them generally easier to manage and understand, especially for those new to React development.
One of the main advantages of using functional components is their simplicity. Without the complexity of managing this or component state (prior to the introduction of hooks), they are straightforward to test and integrate. Additionally, since React 16.8, functional components can fully utilize React hooks to manage state and lifecycle events, making them just as powerful as class components but with cleaner and more readable code.
Here is a basic syntax example:
function Welcome(props) {
return <h1>Hello, {props.name}</h1>;
}
This example defines a functional component named Welcome that takes props and renders a heading with a greeting message. This type of component is useful for displaying simple pieces of the UI without needing to manage a state or lifecycle methods.
Counter Using Functional Components
Now, let’s create a simple counter. This example will show how you can manage state in functional components using hooks, specifically the useState hook.
Here is the complete code for a counter:
import React, { useState } from 'react';
function Counter() {
const [count, setCount] = useState(0);
const incrementCount = () => {
setCount(count + 1);
};
const decrementCount = () => {
setCount(count - 1);
};
return (
<div>
<h1>Current Count: {count}</h1>
<button onClick={incrementCount}>Increase</button>
<button onClick={decrementCount}>Decrease</button>
</div>
);
}
In this example, useState is used to create count state variable. The setCount function allows us to update the count when the buttons are clicked. The UI consists of a heading displaying the current count and two buttons to increase or decrease the count. This counter demonstrates the simplicity and power of functional components for state management in modern React applications.



