ReactEasy

Difference between Functional and Class components?

Function components are plain functions that return JSX and use hooks for state, effects and context. Class components extend React.Component, keep state in this.state, and use lifecycle methods. Function components are the modern standard; class components are legacy but still supported.

Comparison

Function componentClass component
Syntaxfunction Card(props) {}class Card extends React.Component
StateuseState, useReducerthis.state, this.setState
Side effectsuseEffect, useLayoutEffectcomponentDidMount, componentDidUpdate, componentWillUnmount
ContextuseContext / usestatic contextType
this bindingnot neededmust bind handlers or use arrow fields
Logic reusecustom hooksHOCs, render props
React Compiler, Server Componentssupportednot supported

Why function components won

  • Less boilerplate and no this confusion.
  • Custom hooks share logic without wrapper components.
  • Related logic (subscribe and unsubscribe) lives together in one effect instead of being split across lifecycle methods.
  • All new React features (hooks such as useActionState, use, useOptimistic, the React Compiler, Server Components) are for function components.

When you still see classes

  • Error boundaries: there is still no hook for componentDidCatch, so they are written as classes (or with a library like react-error-boundary, or a framework helper).
  • Older codebases. React 19 removed some class-only legacy features: string refs and legacy context (contextTypes).

Code example

tsx
1// Function component
2import { useEffect, useState, Component } from "react";
3
4export function ClockFn() {
5 const [time, setTime] = useState(() => new Date());
6 useEffect(() => {
7 const id = setInterval(() => setTime(new Date()), 1000);
8 return () => clearInterval(id); // cleanup = componentWillUnmount
9 }, []);
10 return <p>{time.toLocaleTimeString()}</p>;
11}
12
13// Equivalent class component
14export class ClockClass extends Component<{}, { time: Date }> {
15 state = { time: new Date() };
16 private id?: ReturnType<typeof setInterval>;
17
18 componentDidMount() {
19 this.id = setInterval(() => this.setState({ time: new Date() }), 1000);
20 }
21 componentWillUnmount() {
22 clearInterval(this.id);
23 }
24 render() {
25 return <p>{this.state.time.toLocaleTimeString()}</p>;
26 }
27}

Practice this question

Open it in the study app to track progress, take notes, listen to the answer and work through the rest of the React set.

Open in study app