A functional component is just a regular JavaScript function following React's component convention: it accepts a single props object and returns JSX. Since the introduction of hooks in React 16.8, functional components can do everything class components can, manage state with useState, run side effects with useEffect, and more, without needing the more verbose class syntax, constructor, this binding, and lifecycle methods that class components require. This is why functional components with hooks are now the standard, recommended approach for essentially all new React code.
1Understanding Functional Components
A functional component is just a regular JavaScript function following React's component convention: it accepts a single props object and returns JSX. Since the introduction of hooks in React 16.8, functional components can do everything class components can, manage state with useState, run side effects with useEffect, and more, without needing the more verbose class syntax, constructor, this binding, and lifecycle methods that class components require. This is why functional components with hooks are now the standard, recommended approach for essentially all new React code.
There's no capability gap left between functional and class components since hooks were introduced — new code should be written as functional components with hooks by default, reserving class components only for maintaining pre-existing legacy code.
function Greeting({ name }) {
return <h1>Hello, {name}!</h1>;
}
// <Greeting name="Ana" />2Practical Example
Here is a real-world application of Functional Components showing how it is used in production React code.
import { useState } from 'react';
function Counter() {
const [count, setCount] = useState(0);
return <button onClick={() => setCount(count + 1)}>Count: {count}</button>;
}3Best Practices
Follow these guidelines when working with Functional Components:
1. Write new components as functional components with hooks by default, rather than reaching for the class component syntax
2. Use useState and useEffect, or other hooks, inside functional components for state and side effects that class components would previously have needed lifecycle methods for
3. Avoid mixing class and functional component styles unnecessarily within the same codebase, for consistency
Tip: There's no capability gap left between functional and class components since hooks were introduced — new code should be written as functional components with hooks by default, reserving class components only for maintaining pre-existing legacy code.
function Greeting({ name }) {
return <h1>Hello, {name}!</h1>;
}
// <Greeting name="Ana" />