LearnThatStack Ace your next interview
React · question
Question 4 of 103

What are the differences between functional and class components?

beginner
← All React questions
Re-explain

The main difference is how they handle state and lifecycle logic. A function component is a plain function that uses Hooks such as useState. A class component extends React.Component and uses this.state and lifecycle methods. Function components are the preferred approach in modern React, and classes are a legacy approach React still supports.

Function components have simpler syntax and are easier to test and debug. They skip some class overhead, like creating instances and binding handlers, but raw performance is about the same.

function MyComponent(props) {
  const [count, setCount] = useState(0);
  
  return <div>{count}</div>;
}

Class components are more verbose and return JSX from render(). They use lifecycle methods such as componentDidMount and have access to the this context.

class MyComponent extends React.Component {
  constructor(props) {
    super(props);
    this.state = { count: 0 };
  }
  
  render() {
    return <div>{this.state.count}</div>;
  }
}

Error boundaries still require a class, because there is no way to write one as a function component.

Rewriting in plainer words…

This answer doesn't lend itself to a diagram - it reads best . No credits were charged.

Why there's no diagram: “”

The interactive diagram is below the answer - jump to diagram ↓ · Below it, the related concept . Jump to it ↓

The diagram below the answer is the concept . Jump to it ↓

Tailored explanation · switch back to · ·
What should the new diagram focus on?
Guided practice for React One question at a time. Answer out loud, get graded, see what you missed.
How well did you know this?
AI:

Saved in this browser - sign in to keep your review list.

How should your speech become text?

Listening… your words appear above as you speak - tap Stop when you're done.

Recording · cr - tap Stop & transcribe when you're done.

Transcribing with AI…

Voice:

Keep going - a few more words and AI can grade it.

Interview lens How interviewers actually use this question

Likely follow-ups

  • How do componentDidMount and componentWillUnmount map to Hooks, and is that mapping exact? useEffect with an empty dependency array plus a cleanup function. Effects sync with props and state, so it is not a one-to-one lifecycle map.
  • Why do class components need their handlers bound, and why don't function components have that problem? A method passed as a callback loses `this`, so you bind it in the constructor or use a class-field arrow. A function reads props and state directly.
  • Can you write an error boundary with Hooks? What do you do in a codebase that is all function components? No Hook replaces getDerivedStateFromError or componentDidCatch. Write one class boundary or use a library like react-error-boundary, and wrap the tree.
  • Each render of a function component sees its own props and state. What bug can that cause? A stale closure, where a callback or effect keeps old values. Fix it with correct dependencies, a functional state update, or a ref.
  • What replaces shouldComponentUpdate or PureComponent in a function component? React.memo does the shallow props check. useMemo and useCallback keep object and function props stable so that check can pass.

What you can say

  1. The main difference is how they handle state and lifecycle logic, with Hooks in a function component and this.state plus lifecycle methods in a class.
  2. A function component is a plain function that takes props, calls a Hook like useState, and returns JSX.
  3. A class component extends React.Component, sets this.state in the constructor, returns JSX from render(), and runs mount logic in methods like componentDidMount.
  4. Function components are the modern default because the syntax is shorter, they're easier to test and debug, and there's no instance to create or handlers to bind.
  5. Raw performance is about the same, so I pick functions for readability, not speed.
  6. The one place I still need a class is an error boundary, because you can't write one as a function component.

Weak answers to avoid

  • Says function components can't hold state That was true before Hooks. Since React 16.8, useState and useEffect give functions state and effects, so calling them 'stateless' sounds out of date.
  • Claims function components are much faster Raw performance is about the same. They skip instance creation and binding, but choose them for simpler code and bring up React.memo if speed comes up.
  • Says classes are deprecated or removed React still supports classes, and error boundaries still need one. Call classes legacy, not dead, and name the one place you'd still write one.
  • Treats useEffect as componentDidMount renamed Effects run after every render whose dependencies changed, and they can return a cleanup. Calling useEffect a renamed lifecycle method hides dependency and cleanup bugs.
Interview lens

Likely follow-ups, what you can say, and the weak answers to avoid.

Sign in free to open it Free account - the lens opens as soon as you're back.

Want a quick review of the fundamentals? See the React cheatsheet.

← Back to all React questions
Pro · $10/mo

88 of 103 React answers are in Pro.

Full answers, code samples, and AI explanations that go simpler or deeper. Cancel anytime.

  • Full answers + code
  • AI explanations, simpler or deeper
  • 1,000 AI credits / month
  • Cancel anytime