Chapter 23 β Functional Components
π Definition
A functional component is a React component written as a JavaScript function that accepts props and returns JSX.
π Explanation
Since React 16.8 introduced Hooks, functional components became the standard for new code. They are:
- Simpler β no
this, no constructor. - Easier to test β they're plain functions.
- More composable β custom hooks let you share logic without wrapper hell.
Class components still exist (mostly in legacy code), but you should default to functions.
π» Code Example β Minimal Functional Component
function Greeting({ name }) {
return <h1>Hello, {name}</h1>;
}
// Usage
<Greeting name="Soumya" />π» Code Example β Arrow Function Variant
const Greeting = ({ name }) => <h1>Hello, {name}</h1>;Both forms are interchangeable.
π» Code Example β With State and Effects
import { useState, useEffect } from "react";
function UserProfile({ userId }) {
const [user, setUser] = useState(null);
const [loading, setLoading] = useState(true);
useEffect(() => {
let active = true;
fetch(`/api/users/${userId}`)
.then(r => r.json())
.then(data => active && setUser(data))
.finally(() => active && setLoading(false));
return () => { active = false; }; // cleanup
}, [userId]);
if (loading) return <p>Loadingβ¦</p>;
if (!user) return <p>Not found</p>;
return <h2>{user.name}</h2>;
}π» Code Example β Class vs Functional (Same Component)
// Class (legacy)
class Counter extends React.Component {
state = { count: 0 };
inc = () => this.setState({ count: this.state.count + 1 });
render() {
return <button onClick={this.inc}>{this.state.count}</button>;
}
}
// Functional (modern)
function Counter() {
const [count, setCount] = useState(0);
return <button onClick={() => setCount(count + 1)}>{count}</button>;
}π» Code Example β Composition
function Avatar({ url }) { return <img src={url} className="avatar" />; }
function UserName({ name }) { return <span className="name">{name}</span>; }
function UserCard({ user }) {
return (
<div className="card">
<Avatar url={user.avatar} />
<UserName name={user.name} />
</div>
);
}π» Code Example β Conditional Rendering
function Status({ isOnline }) {
return (
<div>
{isOnline ? (
<span className="green">Online</span>
) : (
<span className="grey">Offline</span>
)}
{isOnline && <button>Send message</button>}
</div>
);
}π» Code Example β Lists with Keys
function TodoList({ todos }) {
return (
<ul>
{todos.map(todo => (
<li key={todo.id}>{todo.text}</li> // key must be stable + unique
))}
</ul>
);
}π Real-World Impact
- All new components in your Hyscaler projects should be functional.
- Refactor old class components only when you touch them β don't waste cycles on rewrites.
π― Likely Interview Questions
- Functional vs class components?
- What is JSX? β Syntactic sugar over
React.createElement(type, props, children). Babel compiles it. - Can a functional component have state? β Yes, via
useState. - Why are keys needed in lists? β Stable identity for reconciliation diffing.
β Browser APIs & Web Performance | Index | Next: Props vs State β