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 component | Class component | |
|---|---|---|
| Syntax | function Card(props) {} | class Card extends React.Component |
| State | useState, useReducer | this.state, this.setState |
| Side effects | useEffect, useLayoutEffect | componentDidMount, componentDidUpdate, componentWillUnmount |
| Context | useContext / use | static contextType |
this binding | not needed | must bind handlers or use arrow fields |
| Logic reuse | custom hooks | HOCs, render props |
| React Compiler, Server Components | supported | not supported |
Why function components won
- Less boilerplate and no
thisconfusion. - 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 likereact-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 component2import { useEffect, useState, Component } from "react";34export function ClockFn() {5 const [time, setTime] = useState(() => new Date());6 useEffect(() => {7 const id = setInterval(() => setTime(new Date()), 1000);8 return () => clearInterval(id); // cleanup = componentWillUnmount9 }, []);10 return <p>{time.toLocaleTimeString()}</p>;11}1213// Equivalent class component14export class ClockClass extends Component<{}, { time: Date }> {15 state = { time: new Date() };16 private id?: ReturnType<typeof setInterval>;1718 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.