← Back to React, page 1

react-0001 · Easy fresher

What is the difference between functional and class components in React, and which should you write today?

A functional component is a plain function that takes props and returns JSX. It gets state and side effects from hooks. A class component extends Component and uses this.state and lifecycle methods. New code should use functions. Classes still work in React 19, and error boundaries are the one thing that still has to be a class.

Full answer

A component is a reusable piece of UI. It receives props (inputs) and returns what should appear on screen. React lets you write one in two ways.

A functional component is a function. It reads props from its argument, keeps state with useState, and runs side effects with useEffect. A class component extends Component, keeps state in this.state, reads props from this.props, and uses lifecycle methods such as componentDidMount and componentDidUpdate.

Since hooks arrived in React 16.8, functions are the standard choice. Hooks keep related logic together. A timer that starts and stops lives in one useEffect, while a class splits the same job across componentDidMount and componentWillUnmount. Functions also avoid this, bound event handlers and wrapper patterns such as higher-order components when you want to share logic.

Classes were not removed. Old class components run unchanged in React 19. One feature still has no hook version: an error boundary must be a class (or come from a library that wraps one).

Be careful with the claim that functions are "faster". The speed difference is small and not a reason to choose them. The real reasons are less code, easier reuse and a better fit with tools such as the React Compiler.

Here is the same counter written both ways:

jsx
import { Component, useState } from 'react';

function CounterFn() {
  const [count, setCount] = useState(0);
  return <button onClick={() => setCount((c) => c + 1)}>Function: {count}</button>;
}

class CounterClass extends Component {
  state = { count: 0 };
  render() {
    return (
      <button onClick={() => this.setState((s) => ({ count: s.count + 1 }))}>
        Class: {this.state.count}
      </button>
    );
  }
}

export default function Demo() {
  return (
    <>
      <CounterFn />
      <CounterClass />
    </>
  );
}
jsx
import { Component, useEffect, useState } from 'react';

function FunctionalCounter({ name }) {
  const [count, setCount] = useState(0);

  useEffect(() => {
    document.title = `${name}: ${count}`;
  }, [name, count]);

  return (
    <div>
      <h2>Hello, {name}</h2>
      <button onClick={() => setCount((c) => c + 1)}>Clicked {count} times</button>
    </div>
  );
}

class ClassCounter extends Component {
  state = { count: 0 };

  componentDidMount() {
    document.title = `${this.props.name}: ${this.state.count}`;
  }

  componentDidUpdate() {
    document.title = `${this.props.name}: ${this.state.count}`;
  }

  render() {
    return (
      <div>
        <h2>Hello, {this.props.name}</h2>
        <button onClick={() => this.setState((s) => ({ count: s.count + 1 }))}>
          Clicked {this.state.count} times
        </button>
      </div>
    );
  }
}

export default function App() {
  return (
    <>
      <FunctionalCounter name="Function" />
      <ClassCounter name="Class" />
    </>
  );
}

Both components render the same markup and update the document title. The class needs two lifecycle methods for the title, while the function needs one effect with a dependency array. Rendered result after one click on each button: <div><h2>Hello, Function</h2><button>Clicked 1 times</button></div><div><h2>Hello, Class</h2><button>Clicked 1 times</button></div>

Key points

  • A functional component is a function from props to JSX. A class component extends Component and has a render method.
  • Hooks give functions state, effects, context and refs, so there is no need for classes in new code.
  • Classes still run in React 19. Only error boundaries still require a class.
  • Do not claim functions are faster. Say they are shorter and easier to reuse.

Common mistakes

  • Saying class components are deprecated or removed. They are supported, only no longer recommended for new code.
  • Mutating state directly with this.state.count++ in a class instead of calling setState.
  • Forgetting that this inside a normal method is undefined unless the method is an arrow function or is bound.
  • Claiming that functional components are always faster than classes.

Follow-ups

  • Why does an error boundary still need to be a class component?
  • How would you convert componentDidUpdate logic that compares previous props into a hook?
  • What did higher-order components solve, and what replaced them?

Tips

  • Lead with what is recommended today, then mention classes as legacy you can still read and maintain.
  • Offer one concrete reason for hooks (grouping related logic) instead of a vague "it is simpler".
  • If asked about lifecycle, map componentDidMount, componentDidUpdate and componentWillUnmount to useEffect without over-claiming that the mapping is one to one.